markdown-it / markdown-it-container

Fenced container plugin for markdown-it markdown parser
MIT License
496 stars 74 forks source link

is there way to remove container content? #48

Closed Richard-Choooou closed 11 months ago

Richard-Choooou commented 1 year ago

same like.

before:

::: example
there is a content
:::

after render:

there is an other content

without the raw content

Richard-Choooou commented 1 year ago

I tryed to modify the token list

render: (tokens, idx) => {
    if (tokens[idx].nesting === 1) {
        const config = tokens[idx + 2]
        for (let i = 0; i < 3; i++) {
            tokens.splice(idx + 3 - i, 1)
        }
        return `<div>
            <echarts-render echartsConfig="${config.content}"></echarts-render>
        `;
    } else {
        return '</div>';
    }
}

the well be throw error like this.

./docs/line-charts.md
Module build failed (from ./node_modules/@vuepress/markdown-loader/index.js):
TypeError: Cannot read properties of undefined (reading 'type')
    at Renderer.render (C:\Users\zhoup\Desktop\project\front-end\cateyes-charts\node_modules\markdown-it\lib\renderer.js:321:22)
    at MarkdownIt.render (C:\Users\zhoup\Desktop\project\front-end\cateyes-charts\node_modules\markdown-it\lib\index.js:543:24)
    at MarkdownIt.md.render (C:\Users\zhoup\Desktop\project\front-end\cateyes-charts\node_modules\@vuepress\markdown\index.js:142:25)
    at Object.module.exports (C:\Users\zhoup\Desktop\project\front-end\cateyes-charts\node_modules\@vuepress\markdown-loader\index.js:74:16)
CaiWenlie commented 1 year ago

can use a html comment tag:

    if (tokens[idx].nesting === 1) {
        return `<div><!--`;
    } else {
        return '--></div>';
    }