diff options
| author | ruki <waruqi@gmail.com> | 2018-11-08 00:38:48 +0800 |
|---|---|---|
| committer | ruki <waruqi@gmail.com> | 2018-11-07 21:53:09 +0800 |
| commit | 26105034da4fcce7ac883c899d781f016559310d (patch) | |
| tree | c459a5dc4e3aa0972d9919033ece511ce76dd129 /node_modules/vuepress/lib/util/parseHeaders.js | |
| parent | 2c77f00f1a7ecb6c8192f9c16d3b2001b254a107 (diff) | |
| download | xmake-docs-26105034da4fcce7ac883c899d781f016559310d.tar.gz xmake-docs-26105034da4fcce7ac883c899d781f016559310d.zip | |
switch to vuepress
Diffstat (limited to 'node_modules/vuepress/lib/util/parseHeaders.js')
| -rw-r--r-- | node_modules/vuepress/lib/util/parseHeaders.js | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/node_modules/vuepress/lib/util/parseHeaders.js b/node_modules/vuepress/lib/util/parseHeaders.js new file mode 100644 index 00000000..2c9fe641 --- /dev/null +++ b/node_modules/vuepress/lib/util/parseHeaders.js @@ -0,0 +1,56 @@ +// Since VuePress needs to extract the header from the markdown source +// file and display it in the sidebar or title (#238), this file simply +// removes some unnecessary elements to make header displays well at +// sidebar or title. +// +// But header's parsing in the markdown content is done by the markdown +// loader based on markdown-it. markdown-it parser will will always keep +// HTML in headers, so in VuePress, after being parsed by the markdiwn +// loader, the raw HTML in headers will finally be parsed by Vue-loader. +// so that we can write HTML/Vue in the header. One exception is the HTML +// wrapped by <code>(markdown token: '`') tag. + +const { compose } = require('./shared') + +const parseEmojis = str => { + const emojiData = require('markdown-it-emoji/lib/data/full.json') + return String(str).replace(/:(.+?):/g, (placeholder, key) => emojiData[key] || placeholder) +} + +const unescapeHtml = html => String(html) + .replace(/"/g, '"') + .replace(/'/g, '\'') + .replace(/:/g, ':') + .replace(/</g, '<') + .replace(/>/g, '>') + +const removeMarkdownTokens = str => String(str) + .replace(/\[(.*)\]\(.*\)/, '$1') // []() + .replace(/(`|\*{1,3}|_)(.*?[^\\])\1/g, '$2') // `{t}` | *{t}* | **{t}** | ***{t}*** | _{t}_ + .replace(/(\\)(\*|_|`|\!)/g, '$2') // remove escape char '\' + +const trim = str => str.trim() + +// This method remove the raw HTML but reserve the HTML wrapped by `<code>`. +// e.g. +// Input: "<a> b", Output: "b" +// Input: "`<a>` b", Output: "`<a>` b" +exports.removeNonCodeWrappedHTML = (str) => { + return String(str).replace(/(^|[^><`])<.*>([^><`]|$)/g, '$1$2') +} + +// Unescape html, parse emojis and remove some md tokens. +exports.parseHeaders = compose( + unescapeHtml, + parseEmojis, + removeMarkdownTokens, + trim +) + +// Also clean the html that isn't wrapped by code. +// Because we want to support using VUE components in headers. +// e.g. https://vuepress.vuejs.org/guide/using-vue.html#badge +exports.deeplyParseHeaders = compose( + exports.removeNonCodeWrappedHTML, + exports.parseHeaders, +) |
