Skip to content

escape text in code block when lang is 'text' or not specified #31

Closed
@lazzzis

Description

@lazzzis

Content in my markdown file:

Run vuepress dev, it throws an error:

Run vuepress build, it doesn't throw an error. But the page is not rendered as expected:


I guess the cause is that code isn't escaped, when lang is 'text' or not specified.

function wrap (code, lang) {
return `<pre v-pre class="language-${lang}"><code>${code}</code></pre>`
}

Since prism would automatically escape code:

> prism.highlight('<scheme>', prism.languages['html'], 'html')
'<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>scheme</span><span class="token punctuation">></span></span>'

A quick solution might be:

const escapeHtml = require('escape-html')

function wrap (code, lang) {
  if (lang === 'text') {
    code = escapeHtml(code)
  }
  return `<pre v-pre class="language-${lang}"><code>${code}</code></pre>`
}

This solution works well both on vuepress dev and vuepress build. But I don't know if this is a good solution

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions