當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript js.highlightAuto方法代碼示例

本文整理匯總了TypeScript中highlight.js.highlightAuto方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript js.highlightAuto方法的具體用法?TypeScript js.highlightAuto怎麽用?TypeScript js.highlightAuto使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在highlight.js的用法示例。


在下文中一共展示了js.highlightAuto方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: getRenderedSourceCode

async function getRenderedSourceCode(): Promise<string> {
  let printConfig = vscode.workspace.getConfiguration("print", null);
  let printAndClose = printConfig.printAndClose ? " onload = \"window.print();window.close();\"" : "";
  if (printConfig.renderMarkdown && commandArgs.fsPath.split('.').pop().toLowerCase() === "md") {
    let markdownConfig = vscode.workspace.getConfiguration("markdown", null);
    return `<!DOCTYPE html><html><head><title>${commandArgs.fsPath}</title>
    <meta charset="utf-8"/>
    <style>
    html, body {
      font-family: ${markdownConfig.preview.fontFamily};
      font-size: ${markdownConfig.preview.fontSize}px;
      line-height: ${markdownConfig.preview.lineHeight}em;
    }
    img {
      max-width: 100%;
    }
    h1,h2,h3,h4,h5,h6 {
      page-break-after:avoid;
      page-break-inside:avoid;
    }
    </style>
    ${markdownConfig.styles.map((cssFilename: string) => `<link href="${cssFilename}" rel="stylesheet" />`).join("\n")}
    </head>
    <body${printAndClose}>${markdown_it().render(fs.readFileSync(commandArgs.fsPath).toString())}</body></html>`;
  }
  let x = vscode.extensions.getExtension("pdconsec.vscode-print");
  if (!x) { throw new Error("Cannot resolve extension. Has the name changed? It is defined by the publisher and the extension name defined in package.json"); }
  let stylePath = `${x.extensionPath}/node_modules/highlight.js/styles`;
  let defaultCss = getFileText(`${stylePath}/default.css`);
  let swatchCss = getFileText(`${stylePath}/${printConfig.colourScheme}.css`);
  let sourceCode = await getSourceCode();
  let renderedCode = "";
  try {
    renderedCode = hljs.highlight(sourceCode[0], sourceCode[1]).value;
  }
  catch (err) {
    renderedCode = hljs.highlightAuto(sourceCode[1]).value;
  }
  var addLineNumbers = printConfig.lineNumbers === "on" || (printConfig.lineNumbers === "inherit" && vscode.window.activeTextEditor && (vscode.window.activeTextEditor.options.lineNumbers || 0) > 0);
  if (addLineNumbers) {
    var startLine = selection && !(selection.isEmpty || selection.isSingleLine) ? selection.start.line + 1 : 1;
    renderedCode = renderedCode
      .split("\n")
      .map((line, i) => `<tr><td class="line-number">${startLine + i}</td><td class="line-text">${line}</td></tr>`)
      .join("\n")
      .replace("\n</td>", "</td>")
      ;
  } else {
    renderedCode = renderedCode
      .split("\n")
      .map((line, i) => `<tr><td class="line-text">${line}</td></tr>`)
      .join("\n")
      .replace("\n</td>", "</td>")
      ;
  }
  let editorConfig = vscode.workspace.getConfiguration("print", null);
  let html = `<html><head><title>${commandArgs.fsPath}</title><style>body{margin:0;padding:0;tab-size:${editorConfig.tabSize}}\n${defaultCss}\r${swatchCss}\n${lineNumberCss.replace("{lineSpacing}", (printConfig.lineSpacing - 1).toString())}\n.hljs { max-width:100%; width:100%; font-family: Consolas, monospace; font-size: ${printConfig.fontSize}; }\n</style></head><body${printAndClose}><table class="hljs">${renderedCode}</table></body></html>`;
  return html;
}
開發者ID:natkuhn,項目名稱:vsc-print,代碼行數:59,代碼來源:extension.ts

示例2:

 renderer.code = (code: string, language: string) => {
   return `<pre class="hljs lang-${language}"><code class="${language}">${
     // 如果在支持的語言列表裏麵就用該語言渲染,否則使用默認語言渲染
     hljs.getLanguage(language) ?
       hljs.highlight(language, code).value :
       hljs.highlightAuto(code).value
     }</code></pre>`
 }
開發者ID:linkFly6,項目名稱:Said,代碼行數:8,代碼來源:html.ts

示例3: inferLanguage

function inferLanguage(code: string): string {
  if (/<script>/.test(code)) {
    return 'html';
  }
  if (/Polymer\(\s*\{/.test(code)) {
    return 'javascript';
  }
  return highlight.highlightAuto(code, ['html', 'css', 'javascript']).language;
}
開發者ID:TimvdLippe,項目名稱:tedium,代碼行數:9,代碼來源:markdown-lang-autodetect.ts

示例4:

export const codeToHtml = (code: string, { language = null, interpretHints = true } = {}) => {
  if (language) return `<code>${highlight(language, code).value}</code>`;

  if (interpretHints) {
    const langRegex = /^([\w-.]+):\s/g;
    const match = langRegex.exec(code);
    if (match && listLanguages().includes(match[1])) {
      const restOfCode = code.substring(langRegex.lastIndex);
      return `<code>${highlight(match[1], restOfCode).value}</code>`;
    }
  }

  return `<code>${highlightAuto(code).value}</code>`;
};
開發者ID:luketurner,項目名稱:scripsi,代碼行數:14,代碼來源:code.ts

示例5: catch

 highlight: (str: string, lang: string) => {
     if (hljs) {
         if (lang && hljs.getLanguage(lang)) {
             try {
                 return hljs.highlight(lang, str).value;
             } catch (error) {
                 console.log(error);
             }
         }
         try {
             return hljs.highlightAuto(str).value;
         } catch (error) {
             console.log(error);
         }
     }
     return "";
 },
開發者ID:AbraaoAlves,項目名稱:DefinitelyTyped,代碼行數:17,代碼來源:markdown-it-tests.ts

示例6: highlightAuto

 highlight: (code, lang) => {
   return highlightAuto(code, lang ? [lang] : undefined).value;
 }
開發者ID:kevinphelps,項目名稱:kevinphelps.me,代碼行數:3,代碼來源:markdown.pipe.ts

示例7:

 highlight: (code, language) => language
     ? hljs.highlight(language, code).value
     : hljs.highlightAuto(code).value
開發者ID:MikeJerred,項目名稱:homesite,代碼行數:3,代碼來源:markdown.component.ts

示例8:

renderer.code = (code: string, language: string, isEscaped: boolean): string => {
  code = highlight.highlightAuto(code).value
  return `<code class="lang-${language} md-code">${code}</code>`
}
開發者ID:bbxyard,項目名稱:bbxyard,代碼行數:4,代碼來源:md.ts

示例9: highlight

// h1 - h6
renderer.heading = (text: string, level: number, raw: string): string => {
  return `<h${level} class="md-h${level}">${text}</h${level}>`
}

// code
renderer.code = (code: string, language: string, isEscaped: boolean): string => {
  code = highlight.highlightAuto(code).value
  return `<code class="lang-${language} md-code">${code}</code>`
}

// init
marked.setOptions({
  renderer,
  highlight (code: string, lang: string) {
    return highlight.highlightAuto(code, [lang]).value
  }
})

// MD 轉 HTML
function md2html (source: string, isFormat: boolean) {
  source = md.marked(source)

  // 格式化:換行、縮進
  if (isFormat) {
    source = source.replace(/\n/g, '<br/>')
    source = source.replace(/[ ]{2}/g, '<span class="md--tab"></span>')
  }
  return source
}
開發者ID:bbxyard,項目名稱:bbxyard,代碼行數:30,代碼來源:md.ts

示例10: highlight

 public highlight(code: string): string {
   return hljs.highlightAuto(code, ['ts', 'html', 'scss', 'nginx']).value;
 }
開發者ID:kevinheader,項目名稱:nebular,代碼行數:3,代碼來源:highlight.service.ts


注:本文中的highlight.js.highlightAuto方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。