2.2.0 • Published 1 month ago

marked-highlight v2.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month ago

marked-highlight

Highlight code blocks

Installation

npm install marked-highlight

Usage

You will need to provide a function that transforms the code to html.

import { Marked } from "marked";
import { markedHighlight } from "marked-highlight";
import hljs from 'highlight.js';

// or UMD script
// <script src="https://cdn.jsdelivr.net/npm/marked/lib/marked.umd.js"></script>
// <script src="https://cdn.jsdelivr.net/npm/marked-highlight/lib/index.umd.js"></script>
// const {markedHighlight} = globalThis.markedHighlight;
const marked = new Marked(
  markedHighlight({
	emptyLangClass: 'hljs',
    langPrefix: 'hljs language-',
    highlight(code, lang, info) {
      const language = hljs.getLanguage(lang) ? lang : 'plaintext';
      return hljs.highlight(code, { language }).value;
    }
  })
);


marked.parse(`
\`\`\`javascript
const highlight = "code";
\`\`\`
`);
// <pre><code class="hljs language-javascript">
//   <span class="hljs-keyword">const</span> highlight = <span class="hljs-string">&quot;code&quot;</span>;
// </code></pre>

The async option should be set to true if the highlight function returns a Promise.

import { Marked } from "marked";
import { markedHighlight } from "marked-highlight";
import pygmentize from 'pygmentize-bundled';

const marked = new Marked(
  markedHighlight({
    async: true,
    highlight(code, lang, info) {
      return new Promise((resolve, reject) => {
        pygmentize({ lang, format: 'html' }, code, function (err, result) {
          if (err) {
            reject(err);
            return;
          }

          resolve(result.toString());
        });
      });
    }
  })
)

await marked.parse(`
\`\`\`javascript
const highlight = "code";
\`\`\`
`);
// <pre><code class="language-javascript">
//   <div class="highlight">
//     <pre>
//       <span class="kr">const</span> <span class="nx">highlight</span> <span class="o">=</span> <span class="s2">&quot;code&quot;</span><span class="p">;</span>
//     </pre>
//   </div>
// </code></pre>

options

optiontypedefaultdescription
asyncbooleanfalseIf the highlight function returns a promise set this to true. Don't forget to await the call to marked.parse
langPrefixstring'language-'A prefix to add to the class of the code tag.
emptyLangClassstring''The class to add to the code tag if the language is empty.
highlightfunction(code: string, lang: string) => {}Required. The function to transform the code to html.
@98js/markdown@everything-registry/sub-chunk-2133@xcorp/markedextrusive.mdsveltekit-carbon-utilswaline-clientsajtvockervivia-marked@mtti/mhpyide-uimocorjeremy-waline-clienticeforgemacro-mdkoishi-plugin-minoribotkoishi-plugin-chatluna-image-renderergraph-mailermarkdown-latex-renderermarkdown-vue3marked-addonsmarked-addons-testmarkbasemarkugenmd-to-ankipage-deploypostdocnode-slatenetwatch-chat-jsnuxt-openapi-docs-modulereact-m-editorreact-markdown-editor-smdestaircase-custom-componentssimple-m-editorts-dotnetify-elementsuicmpt@appland/components@aegisjsproject/markdown@beforesemicolon/builder@axux/editors@bitmd/plugin-highlight@changke/staticnext-build@difizen/libro-prompt-cellyide-ui-vuextt-ui@hckr_/blendid@fractality/core@hidoo/handlebars-helpers@freecodecamp/freecodecamp-os@gradio/markdown-code@lumjs/doxs@muyajs/core@mpjovanovich/dotmark@mariozechner/blargh@marktext/muya@nathanti/easydocs@mindverse/container@jam-do/jam-tools@jaybeeuu/compost@hyrious/telegraph@graphorigami/origami@react-fabric/mdx@ryan-henness-trimble/mini-docs@tech_query/node-toolkit@oasisdigital/ng-doc-portal@orama/wc-components@osn/previewer@shgysk8zer0/aegis-markdown@shinigamiscans/waline-client@talla-ui/docgen@tdesign-vue-next/chat@roenlie/mirage-mde@dingyi222666/koishi-plugin-chathub@benchristel/mdsite@rumenpetrov/ai-chat-widget@waline/client@verdaccio/ui-components@webcomponents-preview/client@weborigami/origami@xan105/markdown@xsahxl/uiantd-form-dataantd-table-dataarakawaantd-modal-dataadmanbajo-web-mpabeeetle-chatbot-jsbeblobcmcc-chat-embedcuirkember-cli-addon-docsgacofu-copilotfu-copilot-web-xxxxxxgemini4docs
2.2.0

1 month ago

2.1.2

5 months ago

2.1.4

3 months ago

2.1.3

5 months ago

2.1.1

9 months ago

2.1.0

11 months ago

2.0.9

12 months ago

2.0.8

12 months ago

2.0.7

1 year ago

2.0.5

1 year ago

2.0.6

1 year ago

2.0.3

1 year ago

2.0.2

1 year ago

2.0.4

1 year ago

2.0.1

1 year ago

2.0.0

1 year ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago