@vuepress/markdown 2.0.0-beta.4 → 2.0.0-beta.42

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/lib/index.js +5 -1
  2. package/lib/markdown.d.ts +1 -1
  3. package/lib/markdown.js +18 -7
  4. package/lib/plugins/anchorPlugin.d.ts +3 -62
  5. package/lib/plugins/anchorPlugin.js +2 -2
  6. package/lib/plugins/{assetsPlugin.d.ts → assetsPlugin/assetsPlugin.d.ts} +0 -0
  7. package/lib/plugins/assetsPlugin/assetsPlugin.js +32 -0
  8. package/lib/plugins/assetsPlugin/index.d.ts +2 -0
  9. package/lib/plugins/assetsPlugin/index.js +18 -0
  10. package/lib/plugins/assetsPlugin/resolveLink.d.ts +2 -0
  11. package/lib/plugins/assetsPlugin/resolveLink.js +16 -0
  12. package/lib/plugins/codePlugin/codePlugin.d.ts +4 -1
  13. package/lib/plugins/codePlugin/codePlugin.js +24 -20
  14. package/lib/plugins/codePlugin/index.js +5 -1
  15. package/lib/plugins/codePlugin/resolveLanguage.js +2 -3
  16. package/lib/plugins/customComponentPlugin/index.d.ts +2 -0
  17. package/lib/plugins/customComponentPlugin/index.js +7 -1
  18. package/lib/plugins/customComponentPlugin/vueReservedTags.d.ts +1 -1
  19. package/lib/plugins/customComponentPlugin/vueReservedTags.js +1 -1
  20. package/lib/plugins/extractHeadersPlugin.js +3 -2
  21. package/lib/plugins/extractTitlePlugin.d.ts +5 -0
  22. package/lib/plugins/extractTitlePlugin.js +33 -0
  23. package/lib/plugins/hoistTagsPlugin.js +3 -2
  24. package/lib/plugins/importCodePlugin/createImportCodeBlockRule.d.ts +3 -0
  25. package/lib/plugins/importCodePlugin/createImportCodeBlockRule.js +52 -0
  26. package/lib/plugins/importCodePlugin/importCodePlugin.d.ts +8 -0
  27. package/lib/plugins/importCodePlugin/importCodePlugin.js +26 -0
  28. package/lib/plugins/importCodePlugin/index.d.ts +4 -0
  29. package/lib/plugins/importCodePlugin/index.js +20 -0
  30. package/lib/plugins/importCodePlugin/resolveImportCode.d.ts +6 -0
  31. package/lib/plugins/importCodePlugin/resolveImportCode.js +37 -0
  32. package/lib/plugins/importCodePlugin/types.d.ts +5 -0
  33. package/lib/plugins/importCodePlugin/types.js +2 -0
  34. package/lib/plugins/index.d.ts +2 -0
  35. package/lib/plugins/index.js +7 -1
  36. package/lib/plugins/linksPlugin/index.js +5 -1
  37. package/lib/plugins/linksPlugin/linksPlugin.d.ts +18 -1
  38. package/lib/plugins/linksPlugin/linksPlugin.js +44 -50
  39. package/lib/plugins/linksPlugin/resolvePaths.js +3 -1
  40. package/lib/plugins/tocPlugin/createRenderHeaders.js +5 -5
  41. package/lib/plugins/tocPlugin/index.js +5 -1
  42. package/lib/plugins/tocPlugin/tocPlugin.js +5 -3
  43. package/lib/types.d.ts +21 -3
  44. package/lib/utils/index.d.ts +1 -1
  45. package/lib/utils/index.js +6 -2
  46. package/lib/utils/resolveHeadersFromTokens.d.ts +4 -3
  47. package/lib/utils/resolveHeadersFromTokens.js +10 -28
  48. package/lib/utils/resolveTitleFromToken.d.ts +10 -0
  49. package/lib/utils/resolveTitleFromToken.js +37 -0
  50. package/package.json +14 -14
  51. package/CHANGELOG.md +0 -201
  52. package/lib/plugins/assetsPlugin.js +0 -24
  53. package/lib/utils/parseEmoji.d.ts +0 -1
  54. package/lib/utils/parseEmoji.js +0 -6
package/lib/index.js CHANGED
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
5
9
  }) : (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
7
11
  o[k2] = m[k];
package/lib/markdown.d.ts CHANGED
@@ -2,4 +2,4 @@ import type { Markdown, MarkdownOptions } from './types';
2
2
  /**
3
3
  * Create vuepress customized markdown-it instance
4
4
  */
5
- export declare const createMarkdown: ({ anchor, assets, code, customComponent, emoji, extractHeaders, hoistTags, links, toc, ...markdownItOptions }?: MarkdownOptions) => Markdown;
5
+ export declare const createMarkdown: ({ anchor, assets, code, customComponent, emoji, extractHeaders, extractTitle, hoistTags, importCode, links, toc, ...markdownItOptions }?: MarkdownOptions) => Markdown;
package/lib/markdown.js CHANGED
@@ -7,7 +7,7 @@ const utils_1 = require("./utils");
7
7
  /**
8
8
  * Create vuepress customized markdown-it instance
9
9
  */
10
- const createMarkdown = ({ anchor, assets, code, customComponent, emoji, extractHeaders, hoistTags, links, toc, ...markdownItOptions } = {}) => {
10
+ const createMarkdown = ({ anchor, assets, code, customComponent, emoji, extractHeaders, extractTitle, hoistTags, importCode, links, toc, ...markdownItOptions } = {}) => {
11
11
  // create raw markdown-it instance
12
12
  const md = MarkdownIt({
13
13
  ...markdownItOptions,
@@ -17,7 +17,7 @@ const createMarkdown = ({ anchor, assets, code, customComponent, emoji, extractH
17
17
  // =====================================================
18
18
  // following plugins push rules to the end of chain, so
19
19
  // the order to use them is important
20
- // parse emoji (before anchor and toc plugin)
20
+ // parse emoji
21
21
  if (emoji !== false) {
22
22
  md.use(plugins_1.emojiPlugin, emoji);
23
23
  }
@@ -26,13 +26,16 @@ const createMarkdown = ({ anchor, assets, code, customComponent, emoji, extractH
26
26
  md.use(plugins_1.anchorPlugin, {
27
27
  level: [1, 2, 3, 4, 5, 6],
28
28
  slugify: utils_1.slugify,
29
- permalink: true,
30
- permalinkBefore: true,
31
- permalinkSymbol: '#',
29
+ permalink: plugins_1.anchorPlugin.permalink.ariaHidden({
30
+ class: 'header-anchor',
31
+ symbol: '#',
32
+ space: true,
33
+ placement: 'before',
34
+ }),
32
35
  ...anchor,
33
36
  });
34
37
  }
35
- // allow toc syntax (after anchor plugin)
38
+ // allow toc syntax
36
39
  if (toc !== false) {
37
40
  md.use(plugins_1.tocPlugin, {
38
41
  level: [2, 3],
@@ -41,7 +44,7 @@ const createMarkdown = ({ anchor, assets, code, customComponent, emoji, extractH
41
44
  ...toc,
42
45
  });
43
46
  }
44
- // extract headers into env (after anchor plugin)
47
+ // extract headers into env
45
48
  if (extractHeaders !== false) {
46
49
  md.use(plugins_1.extractHeadersPlugin, {
47
50
  level: [2, 3],
@@ -49,6 +52,10 @@ const createMarkdown = ({ anchor, assets, code, customComponent, emoji, extractH
49
52
  ...extractHeaders,
50
53
  });
51
54
  }
55
+ // extract title into env
56
+ if (extractTitle !== false) {
57
+ md.use(plugins_1.extractTitlePlugin);
58
+ }
52
59
  // =====================================================
53
60
  // following plugins modify or replace the rule in place
54
61
  // and have no conflicts, so the order is not important
@@ -72,6 +79,10 @@ const createMarkdown = ({ anchor, assets, code, customComponent, emoji, extractH
72
79
  if (code !== false) {
73
80
  md.use(plugins_1.codePlugin, code);
74
81
  }
82
+ // handle import_code syntax
83
+ if (importCode !== false) {
84
+ md.use(plugins_1.importCodePlugin, importCode);
85
+ }
75
86
  return md;
76
87
  };
77
88
  exports.createMarkdown = createMarkdown;
@@ -1,62 +1,3 @@
1
- import type { PluginWithOptions } from 'markdown-it';
2
- import State = require('markdown-it/lib/rules_core/state_core');
3
- import Token = require('markdown-it/lib/token');
4
- /**
5
- * Options for markdown-it-anchor
6
- *
7
- * @see https://github.com/valeriangalliat/markdown-it-anchor
8
- */
9
- export interface AnchorPluginOptions {
10
- /**
11
- * Minimum level to apply anchors on or array of selected levels
12
- *
13
- * @default 1
14
- */
15
- level?: number | number[];
16
- /**
17
- * A custom slugification function
18
- */
19
- slugify?: (str: string) => string;
20
- /**
21
- * Whether to add permalinks next to titles
22
- *
23
- * @default false
24
- */
25
- permalink?: boolean;
26
- /**
27
- * A custom permalink rendering function
28
- */
29
- renderPermalink?: (slug: string, opts: AnchorPluginOptions, state: State, tokenIndex: number) => void;
30
- /**
31
- * The class of the permalink anchor
32
- */
33
- permalinkClass?: string;
34
- /**
35
- * Place space between the header text and the permalink anchor
36
- */
37
- permalinkSpace?: boolean;
38
- /**
39
- * The symbol in the permalink anchor
40
- */
41
- permalinkSymbol?: string;
42
- /**
43
- * Place the permalink before the title
44
- */
45
- permalinkBefore?: boolean;
46
- /**
47
- * A custom permalink `href` rendering function
48
- */
49
- permalinkHref?: (slug: string, state: State) => string;
50
- /**
51
- * A custom permalink attributes rendering function
52
- */
53
- permalinkAttrs?: (slug: string, state: State) => Record<string, string>;
54
- /**
55
- * Called with token and info after rendering
56
- */
57
- callback?: (token: Token, opts: {
58
- slug: string;
59
- title: string;
60
- }) => void;
61
- }
62
- export declare const anchorPlugin: PluginWithOptions<AnchorPluginOptions>;
1
+ import anchorPlugin from 'markdown-it-anchor';
2
+ export declare type AnchorPluginOptions = anchorPlugin.AnchorOptions;
3
+ export { anchorPlugin };
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.anchorPlugin = void 0;
4
- const rawAnchorPlugin = require("markdown-it-anchor");
5
- exports.anchorPlugin = rawAnchorPlugin;
4
+ const markdown_it_anchor_1 = require("markdown-it-anchor");
5
+ exports.anchorPlugin = markdown_it_anchor_1.default;
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.assetsPlugin = void 0;
4
+ const resolveLink_1 = require("./resolveLink");
5
+ /**
6
+ * Plugin to handle assets links
7
+ */
8
+ const assetsPlugin = (md, { relativePathPrefix = '@source' } = {}) => {
9
+ // wrap raw image renderer rule
10
+ const rawImageRule = md.renderer.rules.image;
11
+ md.renderer.rules.image = (tokens, idx, options, env, self) => {
12
+ const token = tokens[idx];
13
+ // get the image link
14
+ const link = token.attrGet('src');
15
+ if (link) {
16
+ // replace the original link with resolved link
17
+ token.attrSet('src', (0, resolveLink_1.resolveLink)(link, relativePathPrefix, env));
18
+ }
19
+ return rawImageRule(tokens, idx, options, env, self);
20
+ };
21
+ // wrap raw html renderer rule
22
+ const createHtmlRule = (rawHtmlRule) => (tokens, idx, options, env, self) => {
23
+ // replace the original link with resolved link
24
+ tokens[idx].content = tokens[idx].content.replace(/(<img\b.*?src=")([^"]*)(")/gs, (_, prefix, link, suffix) => `${prefix}${(0, resolveLink_1.resolveLink)(link, relativePathPrefix, env)}${suffix}`);
25
+ return rawHtmlRule(tokens, idx, options, env, self);
26
+ };
27
+ const rawHtmlBlockRule = md.renderer.rules.html_block;
28
+ const rawHtmlInlineRule = md.renderer.rules.html_inline;
29
+ md.renderer.rules.html_block = createHtmlRule(rawHtmlBlockRule);
30
+ md.renderer.rules.html_inline = createHtmlRule(rawHtmlInlineRule);
31
+ };
32
+ exports.assetsPlugin = assetsPlugin;
@@ -0,0 +1,2 @@
1
+ export * from './assetsPlugin';
2
+ export * from './resolveLink';
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./assetsPlugin"), exports);
18
+ __exportStar(require("./resolveLink"), exports);
@@ -0,0 +1,2 @@
1
+ import type { MarkdownEnv } from '../../types';
2
+ export declare const resolveLink: (link: string, relativePathPrefix: string, env: MarkdownEnv) => string;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.resolveLink = void 0;
4
+ const utils_1 = require("@vuepress/utils");
5
+ const mdurl_1 = require("mdurl");
6
+ const resolveLink = (link, relativePathPrefix, env) => {
7
+ // decode link to ensure bundler can find the file correctly
8
+ let resolvedLink = (0, mdurl_1.decode)(link);
9
+ // if the link is relative path, and the `env.filePathRelative` exists
10
+ // add `@source` alias to the link
11
+ if (/^\.{1,2}\//.test(link) && env.filePathRelative) {
12
+ resolvedLink = `${relativePathPrefix}/${utils_1.path.join(utils_1.path.dirname(env.filePathRelative), resolvedLink)}`;
13
+ }
14
+ return resolvedLink;
15
+ };
16
+ exports.resolveLink = resolveLink;
@@ -6,8 +6,11 @@ export interface CodePluginOptions {
6
6
  highlightLines?: boolean;
7
7
  /**
8
8
  * Enable line numbers or not
9
+ *
10
+ * - A `boolean` value is to enable line numbers or not.
11
+ * - A `number` value is the minimum number of lines to enable line numbers
9
12
  */
10
- lineNumbers?: boolean;
13
+ lineNumbers?: boolean | number;
11
14
  /**
12
15
  * Wrap the `<pre>` tag with an extra `<div>` or not. Do not disable it unless you
13
16
  * understand what's it for
@@ -11,40 +11,39 @@ const resolveVPre_1 = require("./resolveVPre");
11
11
  const codePlugin = (md, { highlightLines = true, lineNumbers = true, preWrapper = true, vPre = true, } = {}) => {
12
12
  // override default fence renderer
13
13
  md.renderer.rules.fence = (tokens, idx, options, env, slf) => {
14
- var _a, _b, _c;
15
14
  const token = tokens[idx];
16
15
  // get token info
17
16
  const info = token.info ? md.utils.unescapeAll(token.info).trim() : '';
18
- // resolve highlight line ranges from token info
19
- let highlightLinesRanges = null;
20
- if (highlightLines) {
21
- highlightLinesRanges = resolveHighlightLines_1.resolveHighlightLines(info);
22
- }
23
- // resolve line-numbers mark from token info
24
- const useLineNumbers = (_a = resolveLineNumbers_1.resolveLineNumbers(info)) !== null && _a !== void 0 ? _a : lineNumbers;
25
- // resolve v-pre mark from token info
26
- const useVPre = (_b = resolveVPre_1.resolveVPre(info)) !== null && _b !== void 0 ? _b : vPre;
27
17
  // resolve language from token info
28
- const language = resolveLanguage_1.resolveLanguage(info);
29
- // the result of code and lang
30
- let code = token.content;
31
- // try to highlight code
32
- code =
33
- ((_c = options.highlight) === null || _c === void 0 ? void 0 : _c.call(options, code, language.name, '')) || md.utils.escapeHtml(code);
18
+ const language = (0, resolveLanguage_1.resolveLanguage)(info);
34
19
  const languageClass = `${options.langPrefix}${language.name}`;
35
- let result = `<pre${useVPre ? ' v-pre' : ''} class="${languageClass}"><code>${code}</code></pre>`;
20
+ // try to get highlighted code
21
+ const code = options.highlight?.(token.content, language.name, '') ||
22
+ md.utils.escapeHtml(token.content);
23
+ // wrap highlighted code with `<pre>` and `<code>`
24
+ let result = code.startsWith('<pre')
25
+ ? code
26
+ : `<pre class="${languageClass}"><code>${code}</code></pre>`;
27
+ // resolve v-pre mark from token info
28
+ const useVPre = (0, resolveVPre_1.resolveVPre)(info) ?? vPre;
29
+ if (useVPre) {
30
+ result = `<pre v-pre${result.slice('<pre'.length)}`;
31
+ }
36
32
  // if `preWrapper` is disabled, return directly
37
33
  if (!preWrapper) {
38
34
  return result;
39
35
  }
40
36
  // code fences always have an ending `\n`, so we should trim the last line
41
37
  const lines = code.split('\n').slice(0, -1);
38
+ // resolve highlight line ranges from token info
39
+ const highlightLinesRanges = highlightLines
40
+ ? (0, resolveHighlightLines_1.resolveHighlightLines)(info)
41
+ : null;
42
42
  // generate highlight lines
43
43
  if (highlightLinesRanges) {
44
- const ranges = highlightLinesRanges;
45
44
  const highlightLinesCode = lines
46
45
  .map((_, index) => {
47
- if (resolveHighlightLines_1.isHighlightLine(index + 1, ranges)) {
46
+ if ((0, resolveHighlightLines_1.isHighlightLine)(index + 1, highlightLinesRanges)) {
48
47
  return '<div class="highlight-line">&nbsp;</div>';
49
48
  }
50
49
  return '<br>';
@@ -52,13 +51,18 @@ const codePlugin = (md, { highlightLines = true, lineNumbers = true, preWrapper
52
51
  .join('');
53
52
  result = `${result}<div class="highlight-lines">${highlightLinesCode}</div>`;
54
53
  }
54
+ // resolve line-numbers mark from token info
55
+ const useLineNumbers = (0, resolveLineNumbers_1.resolveLineNumbers)(info) ??
56
+ (typeof lineNumbers === 'number'
57
+ ? lines.length >= lineNumbers
58
+ : lineNumbers);
55
59
  // generate line numbers
56
60
  if (useLineNumbers) {
57
61
  // generate line numbers code
58
62
  const lineNumbersCode = lines
59
63
  .map((_, index) => `<span class="line-number">${index + 1}</span><br>`)
60
64
  .join('');
61
- result = `${result}<div class="line-numbers">${lineNumbersCode}</div>`;
65
+ result = `${result}<div class="line-numbers" aria-hidden="true">${lineNumbersCode}</div>`;
62
66
  }
63
67
  result = `<div class="${languageClass} ext-${language.ext}${useLineNumbers ? ' line-numbers-mode' : ''}">${result}</div>`;
64
68
  return result;
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
5
9
  }) : (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
7
11
  o[k2] = m[k];
@@ -27,12 +27,11 @@ const getLanguagesMap = () => {
27
27
  * Resolve language for highlight from token info
28
28
  */
29
29
  const resolveLanguage = (info) => {
30
- var _a, _b;
31
30
  // get user-defined language alias
32
- const alias = ((_a = info.match(/^([a-zA-Z]+)/)) === null || _a === void 0 ? void 0 : _a[1]) || 'text';
31
+ const alias = info.match(/^([^ :[{]+)/)?.[1] || 'text';
33
32
  // if the alias does not have a match in the map
34
33
  // fallback to the alias itself
35
- return ((_b = getLanguagesMap()[alias]) !== null && _b !== void 0 ? _b : {
34
+ return (getLanguagesMap()[alias] ?? {
36
35
  name: alias,
37
36
  ext: alias,
38
37
  aliases: [alias],
@@ -1,4 +1,6 @@
1
1
  export * from './customComponentPlugin';
2
2
  export * from './htmlBlockRule';
3
+ export * from './htmlInlineRule';
4
+ export * from './htmlRe';
3
5
  export * from './inlineTags';
4
6
  export * from './vueReservedTags';
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
5
9
  }) : (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
7
11
  o[k2] = m[k];
@@ -12,5 +16,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
12
16
  Object.defineProperty(exports, "__esModule", { value: true });
13
17
  __exportStar(require("./customComponentPlugin"), exports);
14
18
  __exportStar(require("./htmlBlockRule"), exports);
19
+ __exportStar(require("./htmlInlineRule"), exports);
20
+ __exportStar(require("./htmlRe"), exports);
15
21
  __exportStar(require("./inlineTags"), exports);
16
22
  __exportStar(require("./vueReservedTags"), exports);
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * Vue reserved tags
3
3
  *
4
- * @see https://v3.vuejs.org/api/built-in-components.html
4
+ * @see https://vuejs.org/api/built-in-components.html
5
5
  */
6
6
  export declare const vueReservedTags: string[];
@@ -4,7 +4,7 @@ exports.vueReservedTags = void 0;
4
4
  /**
5
5
  * Vue reserved tags
6
6
  *
7
- * @see https://v3.vuejs.org/api/built-in-components.html
7
+ * @see https://vuejs.org/api/built-in-components.html
8
8
  */
9
9
  exports.vueReservedTags = [
10
10
  'template',
@@ -12,9 +12,10 @@ const extractHeadersPlugin = (md, { level = [2, 3], slugify = utils_1.slugify, f
12
12
  // push the rule to the end of the chain
13
13
  // resolve headers from the parsed tokens
14
14
  md.core.ruler.push('resolveExtractHeaders', (state) => {
15
- headers = utils_1.resolveHeadersFromTokens(state.tokens, {
15
+ headers = (0, utils_1.resolveHeadersFromTokens)(state.tokens, {
16
16
  level,
17
17
  allowHtml: false,
18
+ escapeText: false,
18
19
  slugify,
19
20
  format,
20
21
  });
@@ -24,7 +25,7 @@ const extractHeadersPlugin = (md, { level = [2, 3], slugify = utils_1.slugify, f
24
25
  const render = md.render.bind(md);
25
26
  md.render = (src, env = {}) => {
26
27
  const result = render(src, env);
27
- env.headers = headers !== null && headers !== void 0 ? headers : [];
28
+ env.headers = headers;
28
29
  return result;
29
30
  };
30
31
  };
@@ -0,0 +1,5 @@
1
+ import type { PluginSimple } from 'markdown-it';
2
+ /**
3
+ * Extracting markdown title to env
4
+ */
5
+ export declare const extractTitlePlugin: PluginSimple;
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.extractTitlePlugin = void 0;
4
+ const utils_1 = require("../utils");
5
+ /**
6
+ * Extracting markdown title to env
7
+ */
8
+ const extractTitlePlugin = (md) => {
9
+ let title;
10
+ // push the rule to the end of the chain
11
+ // resolve title from the parsed tokens
12
+ md.core.ruler.push('resolveExtractTitle', (state) => {
13
+ const tokenIdx = state.tokens.findIndex((token) => token.tag === 'h1');
14
+ if (tokenIdx > -1) {
15
+ title = (0, utils_1.resolveTitleFromToken)(state.tokens[tokenIdx + 1], {
16
+ escapeText: false,
17
+ allowHtml: false,
18
+ });
19
+ }
20
+ else {
21
+ title = '';
22
+ }
23
+ return true;
24
+ });
25
+ // extract title to env
26
+ const render = md.render.bind(md);
27
+ md.render = (src, env = {}) => {
28
+ const result = render(src, env);
29
+ env.title = env.frontmatter?.title ?? title;
30
+ return result;
31
+ };
32
+ };
33
+ exports.extractTitlePlugin = extractTitlePlugin;
@@ -12,7 +12,8 @@ const hoistTagsPlugin = (md, { customBlocks = [] } = {}) => {
12
12
  // hoist <script>, <style> and other user defined tags
13
13
  const hoistTags = Array.from(new Set(['script', 'style', ...customBlocks]));
14
14
  const hoistTagsRegexp = new RegExp(`^<(${hoistTags.join('|')})(?=(\\s|>|$))`, 'i');
15
- md.renderer.rules.html_block = (tokens, idx, _, env) => {
15
+ const rawRule = md.renderer.rules.html_block;
16
+ md.renderer.rules.html_block = (tokens, idx, options, env, self) => {
16
17
  const content = tokens[idx].content;
17
18
  const hoistedTags = env.hoistedTags || (env.hoistedTags = []);
18
19
  // record the hoisted tags to env and do not render them
@@ -20,7 +21,7 @@ const hoistTagsPlugin = (md, { customBlocks = [] } = {}) => {
20
21
  hoistedTags.push(content);
21
22
  return '';
22
23
  }
23
- return content;
24
+ return rawRule(tokens, idx, options, env, self);
24
25
  };
25
26
  };
26
27
  exports.hoistTagsPlugin = hoistTagsPlugin;
@@ -0,0 +1,3 @@
1
+ import type { RuleBlock } from 'markdown-it/lib/parser_block';
2
+ import type { ImportCodePluginOptions } from './importCodePlugin';
3
+ export declare const createImportCodeBlockRule: ({ handleImportPath }: ImportCodePluginOptions) => RuleBlock;
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createImportCodeBlockRule = void 0;
4
+ const utils_1 = require("@vuepress/utils");
5
+ // min length of the import code syntax, i.e. '@[code]()'
6
+ const MIN_LENGTH = 9;
7
+ // char codes of '@[code'
8
+ const START_CODES = [64, 91, 99, 111, 100, 101];
9
+ // regexp to match the import syntax
10
+ const SYNTAX_RE = /^@\[code(?:{(?:(\d+)?-(\d+)?)})?(?: ([^\]]+))?\]\(([^)]*)\)/;
11
+ const createImportCodeBlockRule = ({ handleImportPath = (str) => str }) => (state, startLine, endLine, silent) => {
12
+ // if it's indented more than 3 spaces, it should be a code block
13
+ /* istanbul ignore if */
14
+ if (state.sCount[startLine] - state.blkIndent >= 4) {
15
+ return false;
16
+ }
17
+ const pos = state.bMarks[startLine] + state.tShift[startLine];
18
+ const max = state.eMarks[startLine];
19
+ // return false if the length is shorter than min length
20
+ if (pos + MIN_LENGTH > max)
21
+ return false;
22
+ // check if it's matched the start
23
+ for (let i = 0; i < START_CODES.length; i += 1) {
24
+ if (state.src.charCodeAt(pos + i) !== START_CODES[i]) {
25
+ return false;
26
+ }
27
+ }
28
+ // check if it's matched the syntax
29
+ const match = state.src.slice(pos, max).match(SYNTAX_RE);
30
+ if (!match)
31
+ return false;
32
+ // return true as we have matched the syntax
33
+ if (silent)
34
+ return true;
35
+ const [, lineStart, lineEnd, info, importPath] = match;
36
+ const meta = {
37
+ importPath: handleImportPath(importPath),
38
+ lineStart: lineStart ? Number.parseInt(lineStart, 10) : 0,
39
+ lineEnd: lineEnd ? Number.parseInt(lineEnd, 10) : undefined,
40
+ };
41
+ // create a import_code token
42
+ const token = state.push('import_code', 'code', 0);
43
+ // use user specified info, or fallback to file ext
44
+ token.info = info ?? utils_1.path.extname(meta.importPath).slice(1);
45
+ token.markup = '```';
46
+ token.map = [startLine, startLine + 1];
47
+ // store token meta to be used in renderer rule
48
+ token.meta = meta;
49
+ state.line = startLine + 1;
50
+ return true;
51
+ };
52
+ exports.createImportCodeBlockRule = createImportCodeBlockRule;
@@ -0,0 +1,8 @@
1
+ import type { PluginWithOptions } from 'markdown-it';
2
+ export interface ImportCodePluginOptions {
3
+ /**
4
+ * A function to handle the import path
5
+ */
6
+ handleImportPath?: (str: string) => string;
7
+ }
8
+ export declare const importCodePlugin: PluginWithOptions<ImportCodePluginOptions>;
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.importCodePlugin = void 0;
4
+ const createImportCodeBlockRule_1 = require("./createImportCodeBlockRule");
5
+ const resolveImportCode_1 = require("./resolveImportCode");
6
+ const importCodePlugin = (md, options = {}) => {
7
+ // add import_code block rule
8
+ md.block.ruler.before('fence', 'import_code', (0, createImportCodeBlockRule_1.createImportCodeBlockRule)(options), {
9
+ alt: ['paragraph', 'reference', 'blockquote', 'list'],
10
+ });
11
+ // add import_code renderer rule
12
+ md.renderer.rules.import_code = (tokens, idx, options, env, slf) => {
13
+ const token = tokens[idx];
14
+ // use imported code as token content
15
+ const { importFilePath, importCode } = (0, resolveImportCode_1.resolveImportCode)(token.meta, env);
16
+ token.content = importCode;
17
+ // extract imported files to env
18
+ if (importFilePath) {
19
+ const importedFiles = env.importedFiles || (env.importedFiles = []);
20
+ importedFiles.push(importFilePath);
21
+ }
22
+ // render the import_code token as a fence token
23
+ return md.renderer.rules.fence(tokens, idx, options, env, slf);
24
+ };
25
+ };
26
+ exports.importCodePlugin = importCodePlugin;
@@ -0,0 +1,4 @@
1
+ export * from './createImportCodeBlockRule';
2
+ export * from './importCodePlugin';
3
+ export * from './resolveImportCode';
4
+ export * from './types';
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./createImportCodeBlockRule"), exports);
18
+ __exportStar(require("./importCodePlugin"), exports);
19
+ __exportStar(require("./resolveImportCode"), exports);
20
+ __exportStar(require("./types"), exports);
@@ -0,0 +1,6 @@
1
+ import type { MarkdownEnv } from '../../types';
2
+ import type { ImportCodeTokenMeta } from './types';
3
+ export declare const resolveImportCode: ({ importPath, lineStart, lineEnd }: ImportCodeTokenMeta, { filePath }: MarkdownEnv) => {
4
+ importFilePath: string | null;
5
+ importCode: string;
6
+ };