Skip to content

Wrapping sections in <section> tags #2889

Answered by UziTech
ispringle asked this question in Q&A
Discussion options

You must be logged in to vote

You can create an extension that finds the text for each section and renders it with the proper tags surrounding it.

Here is one solution. It probably doesn't work in every situation but it should be a good start.

import { Marked } from './src/marked.js';

// The sectionLevel will help us prevent matching the same header multiple times.
let sectionLevel = 0;

// Creating regular expressions is expensive so we create them once.
// Create 7 sections since that is the maximum heading level.
const sectionRegexps = new Array(7).fill().map((e, i) => new RegExp(`^(#{${i + 1}} )[^]*?(?:\\n(?=\\1)|$)`));

const sectionExtension = {
  extensions: [{
    name: 'sectionBlock',
    level: 'block',
    s…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@ispringle
Comment options

Answer selected by ispringle
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants