From 2892f44af774299bfbd154a2157af7d8a1567500 Mon Sep 17 00:00:00 2001 From: neverland Date: Mon, 17 Jan 2022 15:17:34 +0800 Subject: [PATCH] chore: remove vant-markdown-loader (#10205) --- packages/vant-markdown-loader/README.md | 29 ----- packages/vant-markdown-loader/package.json | 26 ---- .../vant-markdown-loader/src/card-wrapper.js | 16 --- .../vant-markdown-loader/src/extract-demo.js | 42 ------- .../vant-markdown-loader/src/highlight.js | 10 -- packages/vant-markdown-loader/src/index.js | 117 ------------------ .../vant-markdown-loader/src/link-open.js | 18 --- .../vant-markdown-loader/src/md-parser.js | 14 --- .../src/side-effect-tags.js | 14 --- pnpm-lock.yaml | 53 -------- 10 files changed, 339 deletions(-) delete mode 100644 packages/vant-markdown-loader/README.md delete mode 100644 packages/vant-markdown-loader/package.json delete mode 100644 packages/vant-markdown-loader/src/card-wrapper.js delete mode 100644 packages/vant-markdown-loader/src/extract-demo.js delete mode 100644 packages/vant-markdown-loader/src/highlight.js delete mode 100644 packages/vant-markdown-loader/src/index.js delete mode 100644 packages/vant-markdown-loader/src/link-open.js delete mode 100644 packages/vant-markdown-loader/src/md-parser.js delete mode 100644 packages/vant-markdown-loader/src/side-effect-tags.js diff --git a/packages/vant-markdown-loader/README.md b/packages/vant-markdown-loader/README.md deleted file mode 100644 index bac03df92..000000000 --- a/packages/vant-markdown-loader/README.md +++ /dev/null @@ -1,29 +0,0 @@ -# vant-markdown-loader - -Simple and fast vue markdown loader, transform markdown to vue component. - -## Install - -```shell -# with npm -npm i @vant/markdown-loader -D - -# with yarn -yarn add @vant/markdown-loader -D - -# with pnpm -pnpm add @vant/markdown-loader -D -``` - -## Options - -- `enableMetaData`: Default `false`. Whether to use [front-matter](https://github.com/jxson/front-matter) to extract markdown meta data - -- `linkOpen`: Default `true`. Whether to add target="\_blank" to all links - -- `wrapper(html, fm)`: Format the returned content using a custom function - - `html`: The result of [markdown-it](https://github.com/markdown-it/markdown-it)'s render - - `fm`: See [fm(string)](https://github.com/jxson/front-matter#fmstring). If `enableMetaData` option is `false`, the value is `undefined`. - - `attributes` - - `body` - - `frontmatter` diff --git a/packages/vant-markdown-loader/package.json b/packages/vant-markdown-loader/package.json deleted file mode 100644 index 493200b47..000000000 --- a/packages/vant-markdown-loader/package.json +++ /dev/null @@ -1,26 +0,0 @@ -{ - "name": "@vant/markdown-loader", - "version": "4.1.1", - "description": "Simple and fast vue markdown loader", - "main": "src/index.js", - "publishConfig": { - "access": "public", - "registry": "https://registry.npmjs.org/" - }, - "repository": { - "type": "git", - "url": "https://github.com/youzan/vant.git", - "directory": "packages/vant-markdown-loader" - }, - "bugs": "https://github.com/youzan/vant/issues", - "author": "chenjiahan", - "license": "MIT", - "dependencies": { - "front-matter": "^4.0.2", - "highlight.js": "^10.7.1", - "loader-utils": "^2.0.0", - "markdown-it": "^12.0.4", - "markdown-it-anchor": "^8.0.0", - "transliteration": "^2.2.0" - } -} diff --git a/packages/vant-markdown-loader/src/card-wrapper.js b/packages/vant-markdown-loader/src/card-wrapper.js deleted file mode 100644 index 6216cdef9..000000000 --- a/packages/vant-markdown-loader/src/card-wrapper.js +++ /dev/null @@ -1,16 +0,0 @@ -module.exports = function cardWrapper(html) { - const group = html - .replace(/

{ - if (fragment.indexOf('${fragment}`; - } - - return fragment; - }) - .join(''); -}; diff --git a/packages/vant-markdown-loader/src/extract-demo.js b/packages/vant-markdown-loader/src/extract-demo.js deleted file mode 100644 index 384d36963..000000000 --- a/packages/vant-markdown-loader/src/extract-demo.js +++ /dev/null @@ -1,42 +0,0 @@ -const path = require('path'); -const fs = require('fs'); -const os = require('os'); -const parser = require('./md-parser'); - -function hyphenate(str) { - return str.replace(/\B([A-Z])/g, '-$1').toLowerCase(); -} - -module.exports = function extraDemo(content) { - const isWin = /^win/.test(os.platform()); - const markdownDir = path.dirname(this.resourcePath); - const demoLinks = []; - - content = content.replace( - /([\s\S]*?)<\/demo-code>/g, - function (_, attrs, link) { - link = link.trim(); // 去换行符 - const tag = 'demo-code-' + hyphenate(path.basename(link, '.vue')); - let fullLink; - if (isWin) { - fullLink = path.posix.join(...markdownDir.split(path.sep), link); - } else { - fullLink = path.join(markdownDir, link); - } - demoLinks.indexOf(fullLink) === -1 && demoLinks.push(fullLink); - const demoContent = fs.readFileSync(fullLink, { encoding: 'utf8' }); - const demoParseredContent = parser.render( - '```html\n' + demoContent + '\n```' - ); - return ` - - <${tag} /> - - `; - } - ); - - return [content, demoLinks]; -}; diff --git a/packages/vant-markdown-loader/src/highlight.js b/packages/vant-markdown-loader/src/highlight.js deleted file mode 100644 index c5fed8c16..000000000 --- a/packages/vant-markdown-loader/src/highlight.js +++ /dev/null @@ -1,10 +0,0 @@ -const hljs = require('highlight.js'); - -module.exports = function highlight(str, lang) { - if (lang && hljs.getLanguage(lang)) { - // https://github.com/highlightjs/highlight.js/issues/2277 - return hljs.highlight(str, { language: lang, ignoreIllegals: true }).value; - } - - return ''; -}; diff --git a/packages/vant-markdown-loader/src/index.js b/packages/vant-markdown-loader/src/index.js deleted file mode 100644 index ece432bcf..000000000 --- a/packages/vant-markdown-loader/src/index.js +++ /dev/null @@ -1,117 +0,0 @@ -const path = require('path'); -const loaderUtils = require('loader-utils'); -const frontMatter = require('front-matter'); -const parser = require('./md-parser'); -const linkOpen = require('./link-open'); -const cardWrapper = require('./card-wrapper'); -const extractDemo = require('./extract-demo'); -const sideEffectTags = require('./side-effect-tags'); - -function camelize(str) { - return `-${str}`.replace(/-(\w)/g, (_, c) => (c ? c.toUpperCase() : '')); -} - -const sharedVueOptions = `mounted() { - const anchors = [].slice.call(this.$el.querySelectorAll('h2, h3, h4, h5')); - - anchors.forEach(anchor => { - anchor.addEventListener('click', this.scrollToAnchor); - }); - }, - - methods: { - scrollToAnchor(event) { - if (event.target.id) { - this.$router.push({ - name: this.$route.name, - hash: '#' + event.target.id - }) - } - } - }, -`; - -function wrapper(content) { - let demoLinks; - [content, demoLinks] = extractDemo.call(this, content); - content = cardWrapper(content); - - // 不包含 demo-code 的 md 文件,直接使绑定 HTML - if (demoLinks.length === 0) { - content = escape(content); - - return ` - -`; - } - - // 包含 demo-code 的 md 文件,需要走模版渲染 - let styles; - [content, styles] = sideEffectTags(content); - - return ` - - - - -${styles.join('\n')} -`; -} - -module.exports = function (source) { - let options = loaderUtils.getOptions(this) || {}; - this.cacheable && this.cacheable(); - - options = { - wrapper, - linkOpen: true, - ...options, - }; - - let fm; - - if (options.enableMetaData) { - fm = frontMatter(source); - source = fm.body; - } - - if (options.linkOpen) { - linkOpen(parser); - } - - return options.wrapper.call(this, parser.render(source), fm); -}; diff --git a/packages/vant-markdown-loader/src/link-open.js b/packages/vant-markdown-loader/src/link-open.js deleted file mode 100644 index 0e513226a..000000000 --- a/packages/vant-markdown-loader/src/link-open.js +++ /dev/null @@ -1,18 +0,0 @@ -// add target="_blank" to all links -module.exports = function linkOpen(md) { - const defaultRender = - md.renderer.rules.link_open || - function(tokens, idx, options, env, self) { - return self.renderToken(tokens, idx, options); - }; - - md.renderer.rules.link_open = function(tokens, idx, options, env, self) { - const aIndex = tokens[idx].attrIndex('target'); - - if (aIndex < 0) { - tokens[idx].attrPush(['target', '_blank']); // add new attribute - } - - return defaultRender(tokens, idx, options, env, self); - }; -}; diff --git a/packages/vant-markdown-loader/src/md-parser.js b/packages/vant-markdown-loader/src/md-parser.js deleted file mode 100644 index 3940d886f..000000000 --- a/packages/vant-markdown-loader/src/md-parser.js +++ /dev/null @@ -1,14 +0,0 @@ -const MarkdownIt = require('markdown-it'); -const markdownItAnchor = require('markdown-it-anchor'); -const highlight = require('./highlight'); -const { slugify } = require('transliteration'); - -const parser = new MarkdownIt({ - html: true, - highlight, -}).use(markdownItAnchor, { - level: 2, - slugify, -}); - -module.exports = parser; diff --git a/packages/vant-markdown-loader/src/side-effect-tags.js b/packages/vant-markdown-loader/src/side-effect-tags.js deleted file mode 100644 index a7f99fd5b..000000000 --- a/packages/vant-markdown-loader/src/side-effect-tags.js +++ /dev/null @@ -1,14 +0,0 @@ -module.exports = function sideEffectTags(content) { - const styles = []; - - // 从模版中移除 script 标签 - content = content.replace(/[\s\S]*?<\/script>/g, ''); - - // 从模版中移除 style 标签,并收集到 styles 数组中,以转移为 .vue 文件 的 style 标签 - content = content.replace(/([\s\S]*?)<\/style>/g, (_, css) => { - styles.push(``); - return ''; - }); - - return [content, styles]; -}; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 9b6bd54db..3c33b2851 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -218,22 +218,6 @@ importers: devDependencies: release-it: 14.11.6 - packages/vant-markdown-loader: - specifiers: - front-matter: ^4.0.2 - highlight.js: ^10.7.1 - loader-utils: ^2.0.0 - markdown-it: ^12.0.4 - markdown-it-anchor: ^8.0.0 - transliteration: ^2.2.0 - dependencies: - front-matter: 4.0.2 - highlight.js: 10.7.3 - loader-utils: 2.0.2 - markdown-it: 12.2.0 - markdown-it-anchor: 8.4.1_markdown-it@12.2.0 - transliteration: 2.2.0 - packages/vant-markdown-vetur: specifiers: '@types/fs-extra': ^9.0.13 @@ -2280,10 +2264,6 @@ packages: /before-after-hook/2.2.2: resolution: {integrity: sha1-pujKQQKNkO4sJCIvIByQlWCRYT4=, tarball: before-after-hook/download/before-after-hook-2.2.2.tgz} - /big.js/5.2.2: - resolution: {integrity: sha1-ZfCvOC9Xi83HQr2cKB6cstd2gyg=, tarball: big.js/download/big.js-5.2.2.tgz} - dev: false - /bl/4.1.0: resolution: {integrity: sha1-RRU1JkGCvsL7vIOmKrmM8R2fezo=, tarball: bl/download/bl-4.1.0.tgz} dependencies: @@ -3086,11 +3066,6 @@ packages: /emoji-regex/9.2.2: resolution: {integrity: sha1-hAyIA7DYBH9P8M+WMXazLU7z7XI=, tarball: emoji-regex/download/emoji-regex-9.2.2.tgz} - /emojis-list/3.0.0: - resolution: {integrity: sha1-VXBmIEatKeLpFucariYKvf9Pang=, tarball: emojis-list/download/emojis-list-3.0.0.tgz} - engines: {node: '>= 4'} - dev: false - /end-of-stream/1.4.4: resolution: {integrity: sha1-WuZKX0UFe682JuwU2gyl5LJDHrA=, tarball: end-of-stream/download/end-of-stream-1.4.4.tgz} dependencies: @@ -3916,12 +3891,6 @@ packages: resolution: {integrity: sha1-rE5SBHPa5nAS1hiquR7aCby0AP8=, tarball: fraction.js/download/fraction.js-4.1.1.tgz} dev: false - /front-matter/4.0.2: - resolution: {integrity: sha1-sU5U3HRc/XKTSE8yENFepO3X9NU=, tarball: front-matter/download/front-matter-4.0.2.tgz} - dependencies: - js-yaml: 3.14.1 - dev: false - /fs-extra/10.0.0: resolution: {integrity: sha1-n/YbZV3eU/s0qC34S7IUzoAuF8E=, tarball: fs-extra/download/fs-extra-10.0.0.tgz?cache=0&sync_timestamp=1632822706452&other_urls=https%3A%2F%2Fregistry.npmmirror.com%2Ffs-extra%2Fdownload%2Ffs-extra-10.0.0.tgz} engines: {node: '>=12'} @@ -4269,10 +4238,6 @@ packages: hasBin: true dev: false - /highlight.js/10.7.3: - resolution: {integrity: sha1-aXJy45kTVuQMPKxWanTu9oF1ZTE=, tarball: highlight.js/download/highlight.js-10.7.3.tgz} - dev: false - /highlight.js/11.3.1: resolution: {integrity: sha1-gTB47zqlGcYXAPhP6QRyMcXcMpE=, tarball: highlight.js/download/highlight.js-11.3.1.tgz} engines: {node: '>=12.0.0'} @@ -5529,15 +5494,6 @@ packages: strip-bom: 3.0.0 dev: false - /loader-utils/2.0.2: - resolution: {integrity: sha1-1uO0+4GHByGuTghoqxHdY4NowSk=, tarball: loader-utils/download/loader-utils-2.0.2.tgz} - engines: {node: '>=8.9.0'} - dependencies: - big.js: 5.2.2 - emojis-list: 3.0.0 - json5: 2.2.0 - dev: false - /locate-path/2.0.0: resolution: {integrity: sha1-K1aLJl7slExtnA3pw9u7ygNUzY4=, tarball: locate-path/download/locate-path-2.0.0.tgz} engines: {node: '>=4'} @@ -5666,15 +5622,6 @@ packages: markdown-it: 12.2.0 dev: false - /markdown-it-anchor/8.4.1_markdown-it@12.2.0: - resolution: {integrity: sha1-KeVgWT9e24CyX9q4sj+T74qRsx4=, tarball: markdown-it-anchor/download/markdown-it-anchor-8.4.1.tgz} - peerDependencies: - '@types/markdown-it': '*' - markdown-it: '*' - dependencies: - markdown-it: 12.2.0 - dev: false - /markdown-it/12.2.0: resolution: {integrity: sha1-CR9yD9XbIG+A3nqNHxpwNf0NONs=, tarball: markdown-it/download/markdown-it-12.2.0.tgz} hasBin: true