fence.js 672 B

1234567891011121314
  1. // 覆盖默认的 fence 渲染策略
  2. module.exports = md => {
  3. const defaultRender = md.renderer.rules.fence;
  4. md.renderer.rules.fence = (tokens, idx, options, env, self) => {
  5. const token = tokens[idx];
  6. // 判断该 fence 是否在 :::demo 内
  7. const prevToken = tokens[idx - 1];
  8. const isInDemoContainer = prevToken && prevToken.nesting === 1 && prevToken.info.trim().match(/^demo\s*(.*)$/);
  9. if (token.info === 'html' && isInDemoContainer) {
  10. return `<template slot="highlight"><pre v-pre><code class="html">${md.utils.escapeHtml(token.content)}</code></pre></template>`;
  11. }
  12. return defaultRender(tokens, idx, options, env, self);
  13. };
  14. };