Skip to content

Add line numbers to code block #164

Answered by yhatt
fredzett asked this question in Q&A
Sep 10, 2021 · 1 comments · 6 replies
Discussion options

You must be logged in to vote

It cannot only in theme CSS + Marp for VS Code. It's a time to borrow a power of JavaScript with Marp CLI!

Customize a Markdown engine with a Marp plugin. The engine in below has written a plugin inline.

// engine.js
module.exports = ({ marp }) =>
  marp.use(({ marpit }) => {
    const { highlighter } = marpit

    // Override Marp Core's highlighter to wrap each lines by ordered list items
    marpit.highlighter = (...args) => {
      const original = highlighter(...args)
      const listItems = original
        .split(/\n(?!$)/) // Don't split at the trailing newline
        .map((line) => `<li>${line}</li>`)

      return `<ol>${listItems.join('')}</ol>`
    }
  })

And contain its styl…

Replies: 1 comment 6 replies

Comment options

You must be logged in to vote
6 replies
@yhatt
Comment options

@yhatt
Comment options

@fredzett
Comment options

@j2deme
Comment options

@yhatt
Comment options

Answer selected by fredzett
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
3 participants