vuejs / theme

VitePress theme for vuejs.org.
MIT License
245 stars 87 forks source link

chore: use tiny-decode instead of v-html+escapeHtml #108

Closed brc-dd closed 5 months ago

brc-dd commented 5 months ago

fixes the concerns in #106

/cc @Jinjiang you can try something like this in demo/guide/introduction.md to test:

## What &amp; &#123;&#123; &lt;script&gt; (1) &#125;&#125; \<script setup> is Vue.js?

The browser bundle only has this code (<200B after gzip/br):

const n=/&([a-z0-9]+|#[0-9]{1,6}|#x[0-9a-fA-F]{1,6});?/gi;let t;function o(e){return t||(t=new DOMParser),t.parseFromString(e,"text/html").documentElement.textContent}function c(e){return n.lastIndex=0,e.replace(n,r=>o(r))

unminified:

const ENTITY_RE = /&([a-z0-9]+|#[0-9]{1,6}|#x[0-9a-fA-F]{1,6});?/gi;
let p;
function decodeHTML5(value) {
  if (!p)
    p = new DOMParser();
  return p.parseFromString(value, "text/html").documentElement.textContent;
}
function decode(value) {
  ENTITY_RE.lastIndex = 0;
  return value.replace(ENTITY_RE, (m) => decodeHTML5(m));
}