mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-05 19:41:42 +08:00
chore: remove vant-markdown-loader (#10205)
This commit is contained in:
parent
71a2e26259
commit
2892f44af7
@ -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`
|
@ -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"
|
||||
}
|
||||
}
|
@ -1,16 +0,0 @@
|
||||
module.exports = function cardWrapper(html) {
|
||||
const group = html
|
||||
.replace(/<h3/g, ':::<h3')
|
||||
.replace(/<h2/g, ':::<h2')
|
||||
.split(':::');
|
||||
|
||||
return group
|
||||
.map(fragment => {
|
||||
if (fragment.indexOf('<h3') !== -1) {
|
||||
return `<div class="card">${fragment}</div>`;
|
||||
}
|
||||
|
||||
return fragment;
|
||||
})
|
||||
.join('');
|
||||
};
|
@ -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(
|
||||
/<demo-code([\s\S]*?)>([\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 `
|
||||
<demo-playground${attrs}
|
||||
origin-code="${escape(demoContent)}"
|
||||
code-snippet="${escape(demoParseredContent)}">
|
||||
<${tag} />
|
||||
</demo-playground>
|
||||
`;
|
||||
}
|
||||
);
|
||||
|
||||
return [content, demoLinks];
|
||||
};
|
@ -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 '';
|
||||
};
|
@ -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 `
|
||||
<script>
|
||||
import { h } from 'vue';
|
||||
|
||||
const content = unescape(\`${content}\`);
|
||||
|
||||
export default {
|
||||
${sharedVueOptions}
|
||||
|
||||
render() {
|
||||
return h('section', { innerHTML: content });
|
||||
}
|
||||
};
|
||||
</script>
|
||||
`;
|
||||
}
|
||||
|
||||
// 包含 demo-code 的 md 文件,需要走模版渲染
|
||||
let styles;
|
||||
[content, styles] = sideEffectTags(content);
|
||||
|
||||
return `
|
||||
<template>
|
||||
<section v-once>
|
||||
${content}
|
||||
</section>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
${demoLinks
|
||||
.map((link) => {
|
||||
return `import DemoCode${camelize(
|
||||
path.basename(link, '.vue')
|
||||
)} from '${link}';`;
|
||||
})
|
||||
.join('\n')}
|
||||
|
||||
export default {
|
||||
components: {
|
||||
${demoLinks
|
||||
.map((link) => `DemoCode${camelize(path.basename(link, '.vue'))}`)
|
||||
.join(',')}
|
||||
},
|
||||
|
||||
${sharedVueOptions}
|
||||
};
|
||||
</script>
|
||||
|
||||
${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);
|
||||
};
|
@ -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);
|
||||
};
|
||||
};
|
@ -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;
|
@ -1,14 +0,0 @@
|
||||
module.exports = function sideEffectTags(content) {
|
||||
const styles = [];
|
||||
|
||||
// 从模版中移除 script 标签
|
||||
content = content.replace(/<script[\s\S]*?>[\s\S]*?<\/script>/g, '');
|
||||
|
||||
// 从模版中移除 style 标签,并收集到 styles 数组中,以转移为 .vue 文件 的 style 标签
|
||||
content = content.replace(/<style[\s\S]*?>([\s\S]*?)<\/style>/g, (_, css) => {
|
||||
styles.push(`<style scoped>${css}</style>`);
|
||||
return '';
|
||||
});
|
||||
|
||||
return [content, styles];
|
||||
};
|
53
pnpm-lock.yaml
generated
53
pnpm-lock.yaml
generated
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user