Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

marked-code-preview

bent103.8kMIT1.3.7TypeScript support: included

A Marked extension for transforming code blocks into code previews

marked, marked-extension, code, codefence, markdown, html, preview, snippet, template, stilearning-marked-extensions

readme

marked-code-preview

A marked extension to transform code blocks within Markdown documents into code previews. It allows you to generate visually appealing code previews for your code snippets within your Markdown content. You can customize the appearance and behavior of code previews using templates and options.

Install

You can install marked-code-preview using npm or yarn:

npm i marked-code-preview
# or
yarn add marked-code-preview

Usage

To use this extension in your Marked processing pipeline, you need to configure your marked processor with this extension. Here's an example of how to do it:

Browser

Say we have the following file example.html:

<!doctype html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Marked code preview extension</title>
  </head>
  <body>
    <div id="content"></div>

    <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/moo/moo.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/json-loose/dist/index.umd.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/attributes-parser/dist/index.umd.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/marked-code-preview/dist/index.umd.min.js"></script>
    <script>
      // 🚨 Important: The `preview` attribute must be specified in code fence blocks.
      const md = `# Example

\`\`\`html preview title="Code title"
<div class='foo'>Hello, World!</div>
\`\`\`
`

      document.getElementById('content').innerHTML = new marked.Marked()
        .use(markedCodePreview())
        .parse(md)
    </script>
  </body>
</html>

Try marked-code-preview on RunKit

Node.js

Say we have the following file example.md:

# Example

```html preview title="Code title"
<div class="foo">Hello, World!</div>
```

🚨 Important: The preview attribute must be specified in code fence blocks.

And our module example.js looks as follows:

import { readFileSync } from 'node:fs'
import { marked } from 'marked'
import markedCodePreview from 'marked-code-preview'

const html = marked
  .use({ gfm: true })
  .use(markedCodePreview)
  .parse(readFileSync('example.md', 'utf8'))

console.log(html)

Now, running node example.js yields:

<h1>Example</h1>

<figure class="preview">
  <figcaption>Code title</figcaption>
  <div class="preview-showcase">
    <div class="foo">Hello, World!</div>
  </div>
  <div class="preview-code">
    <pre><code class="language-html">&lt;div class=&#39;foo&#39;&gt;Hello, World!&lt;/div&gt;
</code></pre>
  </div>
</figure>

Options

This extension accepts the following options:

template?: string

The code preview template to use. You can customize the preview layout using placeholders like {preview}, {code}, codefence meta data (e.g. {title}), and your custom data.

The default template looks like this:

<figure class="preview">
  <figcaption>{title}</figcaption>
  <div class="preview-showcase">{preview}</div>
  <div class="preview-code">{code}</div>
</figure>

You can customize the template according to your needs. For example:

import { readFileSync } from 'node:fs'
import { marked } from 'marked'
import markedCodePreview from 'marked-code-preview'

const customTemplate = `
<figure>
  <div class='preview-container'>
    {preview}
  </div>
  <figcaption>{title}</figcaption>
</figure>
`

const html = marked
  .use({ gfm: true })
  .use(markedCodePreview({ template: customTemplate }))
  .parse(readFileSync('example.md', 'utf8'))

console.log(html)

Yields:

<h1>Example</h1>

<figure>
  <div class="preview-container">
    <div class="foo">Hello, World!</div>
  </div>
  <figcaption>Code title</figcaption>
</figure>

transformer?: Transformer

The transformer function for modifying preview code before replacing the {preview} placeholder.

Default transformer:

;(code, attrs, data) => code

Contributing

We 💛  issues.

When committing, please conform to the semantic-release commit standards. Please install commitizen and the adapter globally, if you have not already.

npm i -g commitizen cz-conventional-changelog

Now you can use git cz or just cz instead of git commit when committing. You can also use git-cz, which is an alias for cz.

git add . && git cz

License

GitHub

A project by Stilearning © 2023-2024.

changelog

marked-code-preview 1.3.7 (2024-08-29)

Bug Fixes

  • deps: update attribute-parser to v2.2.3 (e5d270a)
  • lint code (c75dfc9)

marked-code-preview 1.3.6 (2024-02-03)

Bug Fixes

marked-code-preview 1.3.5 (2023-12-16)

Bug Fixes

marked-code-preview 1.3.4 (2023-11-13)

Bug Fixes

  • calls transformer before interpolation (18fe7b5)

marked-code-preview 1.3.3 (2023-11-03)

Bug Fixes

  • resolves wrong transformer args (a690bb1)

marked-code-preview 1.3.2 (2023-10-26)

Bug Fixes

  • deps: bump attributes-parser (6c22b89)

marked-code-preview 1.3.1 (2023-10-26)

Bug Fixes

  • resolves ERR_REQUIRE_ESM in CommonJS mocules (f876e00)

marked-code-preview 1.3.0 (2023-10-20)

Bug Fixes

  • resolves umd mime issue (f98d31a)

Features

  • build for the esm, cjs, and umd formats (30bc148)

Reverts

marked-code-preview 1.2.1 (2023-10-14)

Bug Fixes

marked-code-preview 1.2.0 (2023-10-02)

Bug Fixes

Features

  • add transformer option (0a9dd61)
  • removes data related options (3095e16)

marked-code-preview 1.1.0 (2023-10-01)

Features

  • gives access to the hooks data through the Lexer object (bd55ce7)

marked-code-preview 1.0.1 (2023-09-29)

Bug Fixes

  • deps: bundled pupa for ssr build (799f5e1)

marked-code-preview 1.0.0 (2023-09-26)

Features