@vuepress/markdown 2.0.0-beta.50-pre → 2.0.0-beta.51

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 (3) hide show
  1. package/dist/index.d.ts +184 -0
  2. package/dist/index.js +561 -0
  3. package/package.json +12 -12
@@ -0,0 +1,184 @@
1
+ import { SfcPluginOptions } from '@mdit-vue/plugin-sfc';
2
+ export { MarkdownSfcBlocks, SfcPluginOptions, sfcPlugin } from '@mdit-vue/plugin-sfc';
3
+ import { MarkdownItEnv } from '@mdit-vue/types';
4
+ import { PageHeader, PageFrontmatter } from '@vuepress/shared';
5
+ import MarkdownIt, { PluginWithOptions } from 'markdown-it';
6
+ import { FrontmatterPluginOptions } from '@mdit-vue/plugin-frontmatter';
7
+ export { FrontmatterPluginOptions, frontmatterPlugin } from '@mdit-vue/plugin-frontmatter';
8
+ import { HeadersPluginOptions } from '@mdit-vue/plugin-headers';
9
+ export { HeadersPluginOptions, headersPlugin } from '@mdit-vue/plugin-headers';
10
+ import { TocPluginOptions } from '@mdit-vue/plugin-toc';
11
+ export { TocPluginOptions, tocPlugin } from '@mdit-vue/plugin-toc';
12
+ import anchorPlugin from 'markdown-it-anchor';
13
+ export { default as anchorPlugin } from 'markdown-it-anchor';
14
+ import emojiPlugin from 'markdown-it-emoji';
15
+ export { default as emojiPlugin } from 'markdown-it-emoji';
16
+ export { ComponentPluginOptions, componentPlugin } from '@mdit-vue/plugin-component';
17
+ export { titlePlugin } from '@mdit-vue/plugin-title';
18
+
19
+ declare type AnchorPluginOptions = anchorPlugin.AnchorOptions;
20
+
21
+ interface AssetsPluginOptions {
22
+ /**
23
+ * Prefix to add to relative assets links
24
+ */
25
+ relativePathPrefix?: string;
26
+ }
27
+ /**
28
+ * Plugin to handle assets links
29
+ */
30
+ declare const assetsPlugin: PluginWithOptions<AssetsPluginOptions>;
31
+
32
+ interface CodePluginOptions {
33
+ /**
34
+ * Enable highlight lines or not
35
+ */
36
+ highlightLines?: boolean;
37
+ /**
38
+ * Enable line numbers or not
39
+ *
40
+ * - A `boolean` value is to enable line numbers or not.
41
+ * - A `number` value is the minimum number of lines to enable line numbers
42
+ */
43
+ lineNumbers?: boolean | number;
44
+ /**
45
+ * Wrap the `<pre>` tag with an extra `<div>` or not. Do not disable it unless you
46
+ * understand what's it for
47
+ *
48
+ * - Required for `highlightLines`
49
+ * - Required for `lineNumbers`
50
+ * - Required for language display of default theme
51
+ */
52
+ preWrapper?: boolean;
53
+ /**
54
+ * Add `v-pre` directive or not
55
+ */
56
+ vPre?: {
57
+ /**
58
+ * Add `v-pre` directive to `<pre>` tag of code block or not
59
+ */
60
+ block?: boolean;
61
+ /**
62
+ * Add `v-pre` directive to `<code>` tag of inline code or not
63
+ */
64
+ inline?: boolean;
65
+ };
66
+ }
67
+ /**
68
+ * Code plugin
69
+ */
70
+ declare const codePlugin: PluginWithOptions<CodePluginOptions>;
71
+
72
+ declare type EmojiPluginOptions = emojiPlugin.Options;
73
+
74
+ interface ImportCodePluginOptions {
75
+ /**
76
+ * A function to handle the import path
77
+ */
78
+ handleImportPath?: (str: string) => string;
79
+ }
80
+ declare const importCodePlugin: PluginWithOptions<ImportCodePluginOptions>;
81
+
82
+ interface LinksPluginOptions {
83
+ /**
84
+ * Tag for internal links
85
+ *
86
+ * @default 'RouterLink'
87
+ */
88
+ internalTag?: 'a' | 'RouterLink';
89
+ /**
90
+ * Additional attributes for external links
91
+ *
92
+ * @default
93
+ * ```js
94
+ * ({
95
+ * target: '_blank',
96
+ * rel: 'noopener noreferrer',
97
+ * })
98
+ * ```
99
+ */
100
+ externalAttrs?: Record<string, string>;
101
+ }
102
+ /**
103
+ * Process links in markdown file
104
+ *
105
+ * - internal links: convert them into `<RouterLink>`
106
+ * - external links: add extra attrs and external icon
107
+ */
108
+ declare const linksPlugin: PluginWithOptions<LinksPluginOptions>;
109
+
110
+ declare type Markdown = MarkdownIt;
111
+
112
+ interface MarkdownOptions extends MarkdownIt.Options {
113
+ anchor?: false | AnchorPluginOptions;
114
+ assets?: false | AssetsPluginOptions;
115
+ code?: false | CodePluginOptions;
116
+ component?: false;
117
+ emoji?: false | EmojiPluginOptions;
118
+ frontmatter?: false | FrontmatterPluginOptions;
119
+ headers?: false | HeadersPluginOptions;
120
+ title?: false;
121
+ importCode?: false | ImportCodePluginOptions;
122
+ links?: false | LinksPluginOptions;
123
+ sfc?: false | SfcPluginOptions;
124
+ slugify?: MarkdownSlugifyFunction;
125
+ toc?: false | TocPluginOptions;
126
+ }
127
+ /**
128
+ * Headers in markdown file
129
+ */
130
+ declare type MarkdownHeader = PageHeader;
131
+ /**
132
+ * Internal links in markdown file
133
+ *
134
+ * Used for file existence check
135
+ */
136
+ interface MarkdownLink {
137
+ raw: string;
138
+ relative: string;
139
+ absolute: string;
140
+ }
141
+ /**
142
+ * The `env` object to be passed to markdown-it render function
143
+ *
144
+ * Input some meta data for markdown file parsing and rendering
145
+ *
146
+ * Output some resources from the markdown file
147
+ */
148
+ interface MarkdownEnv extends MarkdownItEnv {
149
+ /**
150
+ * Base / publicPath of current site
151
+ */
152
+ base?: string;
153
+ /**
154
+ * Absolute file path of the markdown file
155
+ */
156
+ filePath?: string | null;
157
+ /**
158
+ * Relative file path of the markdown file
159
+ */
160
+ filePathRelative?: string | null;
161
+ /**
162
+ * Frontmatter of the markdown file
163
+ */
164
+ frontmatter?: PageFrontmatter;
165
+ /**
166
+ * Imported file that extracted by importCodePlugin
167
+ */
168
+ importedFiles?: string[];
169
+ /**
170
+ * Links that extracted by linksPlugin
171
+ */
172
+ links?: MarkdownLink[];
173
+ }
174
+ /**
175
+ * Type of `slugify` function
176
+ */
177
+ declare type MarkdownSlugifyFunction = (str: string) => string;
178
+
179
+ /**
180
+ * Create vuepress customized markdown-it instance
181
+ */
182
+ declare const createMarkdown: ({ anchor, assets, code, component, emoji, frontmatter, headers, title, importCode, links, sfc, slugify, toc, ...markdownItOptions }?: MarkdownOptions) => Markdown;
183
+
184
+ export { AnchorPluginOptions, AssetsPluginOptions, CodePluginOptions, EmojiPluginOptions, ImportCodePluginOptions, LinksPluginOptions, Markdown, MarkdownEnv, MarkdownHeader, MarkdownLink, MarkdownOptions, MarkdownSlugifyFunction, assetsPlugin, codePlugin, createMarkdown, importCodePlugin, linksPlugin };
package/dist/index.js ADDED
@@ -0,0 +1,561 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __export = (target, all) => {
3
+ for (var name in all)
4
+ __defProp(target, name, { get: all[name], enumerable: true });
5
+ };
6
+
7
+ // src/markdown.ts
8
+ import { slugify as defaultSlugify } from "@mdit-vue/shared";
9
+ import MarkdownIt from "markdown-it";
10
+
11
+ // src/plugins.ts
12
+ import {
13
+ componentPlugin
14
+ } from "@mdit-vue/plugin-component";
15
+ import {
16
+ frontmatterPlugin
17
+ } from "@mdit-vue/plugin-frontmatter";
18
+ import {
19
+ headersPlugin
20
+ } from "@mdit-vue/plugin-headers";
21
+ import { sfcPlugin } from "@mdit-vue/plugin-sfc";
22
+ import { titlePlugin } from "@mdit-vue/plugin-title";
23
+ import { tocPlugin } from "@mdit-vue/plugin-toc";
24
+
25
+ // src/plugins/anchorPlugin.ts
26
+ import { default as anchorPlugin } from "markdown-it-anchor";
27
+
28
+ // src/plugins/assetsPlugin/resolveLink.ts
29
+ import { path } from "@vuepress/utils";
30
+ import { decode } from "mdurl";
31
+ var resolveLink = (link, relativePathPrefix, env) => {
32
+ let resolvedLink = decode(link);
33
+ if (/^\.{1,2}\//.test(link) && env.filePathRelative) {
34
+ resolvedLink = `${relativePathPrefix}/${path.join(
35
+ path.dirname(env.filePathRelative),
36
+ resolvedLink
37
+ )}`;
38
+ }
39
+ return resolvedLink;
40
+ };
41
+
42
+ // src/plugins/assetsPlugin/assetsPlugin.ts
43
+ var assetsPlugin = (md, { relativePathPrefix = "@source" } = {}) => {
44
+ const rawImageRule = md.renderer.rules.image;
45
+ md.renderer.rules.image = (tokens, idx, options, env, self) => {
46
+ const token = tokens[idx];
47
+ const link = token.attrGet("src");
48
+ if (link) {
49
+ token.attrSet("src", resolveLink(link, relativePathPrefix, env));
50
+ }
51
+ return rawImageRule(tokens, idx, options, env, self);
52
+ };
53
+ const createHtmlRule = (rawHtmlRule) => (tokens, idx, options, env, self) => {
54
+ tokens[idx].content = tokens[idx].content.replace(
55
+ /(<img\b.*?src=")([^"]*)(")/gs,
56
+ (_, prefix, src, suffix) => `${prefix}${resolveLink(
57
+ src.trim(),
58
+ relativePathPrefix,
59
+ env
60
+ )}${suffix}`
61
+ ).replace(
62
+ /(<img\b.*?srcset=")([^"]*)(")/gs,
63
+ (_, prefix, srcset, suffix) => `${prefix}${srcset.split(",").map(
64
+ (item) => item.trim().replace(
65
+ /^([^ ]*?)([ \n].*)?$/,
66
+ (_2, url, descriptor = "") => `${resolveLink(
67
+ url.trim(),
68
+ relativePathPrefix,
69
+ env
70
+ )}${descriptor.replace(/[ \n]+/g, " ").trimEnd()}`
71
+ )
72
+ ).join(", ")}${suffix}`
73
+ );
74
+ return rawHtmlRule(tokens, idx, options, env, self);
75
+ };
76
+ const rawHtmlBlockRule = md.renderer.rules.html_block;
77
+ const rawHtmlInlineRule = md.renderer.rules.html_inline;
78
+ md.renderer.rules.html_block = createHtmlRule(rawHtmlBlockRule);
79
+ md.renderer.rules.html_inline = createHtmlRule(rawHtmlInlineRule);
80
+ };
81
+
82
+ // src/plugins/codePlugin/resolveHighlightLines.ts
83
+ var resolveHighlightLines = (info) => {
84
+ const match = info.match(/{([\d,-]+)}/);
85
+ if (match === null) {
86
+ return null;
87
+ }
88
+ return match[1].split(",").map((item) => {
89
+ const range = item.split("-");
90
+ if (range.length === 1) {
91
+ range.push(range[0]);
92
+ }
93
+ return range.map((str) => Number.parseInt(str, 10));
94
+ });
95
+ };
96
+ var isHighlightLine = (lineNumber, ranges) => ranges.some(([start, end]) => lineNumber >= start && lineNumber <= end);
97
+
98
+ // src/plugins/codePlugin/languages.ts
99
+ var languages_exports = {};
100
+ __export(languages_exports, {
101
+ languageBash: () => languageBash,
102
+ languageCsharp: () => languageCsharp,
103
+ languageDocker: () => languageDocker,
104
+ languageFsharp: () => languageFsharp,
105
+ languageJavascript: () => languageJavascript,
106
+ languageKotlin: () => languageKotlin,
107
+ languageMarkdown: () => languageMarkdown,
108
+ languagePython: () => languagePython,
109
+ languageRuby: () => languageRuby,
110
+ languageRust: () => languageRust,
111
+ languageStylus: () => languageStylus,
112
+ languageTypescript: () => languageTypescript,
113
+ languageYaml: () => languageYaml
114
+ });
115
+ var languageBash = {
116
+ name: "bash",
117
+ ext: "sh",
118
+ aliases: ["bash", "sh", "shell", "zsh"]
119
+ };
120
+ var languageCsharp = {
121
+ name: "csharp",
122
+ ext: "cs",
123
+ aliases: ["cs", "csharp"]
124
+ };
125
+ var languageDocker = {
126
+ name: "docker",
127
+ ext: "docker",
128
+ aliases: ["docker", "dockerfile"]
129
+ };
130
+ var languageFsharp = {
131
+ name: "fsharp",
132
+ ext: "fs",
133
+ aliases: ["fs", "fsharp"]
134
+ };
135
+ var languageJavascript = {
136
+ name: "javascript",
137
+ ext: "js",
138
+ aliases: ["javascript", "js"]
139
+ };
140
+ var languageKotlin = {
141
+ name: "kotlin",
142
+ ext: "kt",
143
+ aliases: ["kotlin", "kt"]
144
+ };
145
+ var languageMarkdown = {
146
+ name: "markdown",
147
+ ext: "md",
148
+ aliases: ["markdown", "md"]
149
+ };
150
+ var languagePython = {
151
+ name: "python",
152
+ ext: "py",
153
+ aliases: ["py", "python"]
154
+ };
155
+ var languageRuby = {
156
+ name: "ruby",
157
+ ext: "rb",
158
+ aliases: ["rb", "ruby"]
159
+ };
160
+ var languageRust = {
161
+ name: "rust",
162
+ ext: "rs",
163
+ aliases: ["rs", "rust"]
164
+ };
165
+ var languageStylus = {
166
+ name: "stylus",
167
+ ext: "styl",
168
+ aliases: ["styl", "stylus"]
169
+ };
170
+ var languageTypescript = {
171
+ name: "typescript",
172
+ ext: "ts",
173
+ aliases: ["ts", "typescript"]
174
+ };
175
+ var languageYaml = {
176
+ name: "yaml",
177
+ ext: "yml",
178
+ aliases: ["yaml", "yml"]
179
+ };
180
+
181
+ // src/plugins/codePlugin/resolveLanguage.ts
182
+ var languagesMap;
183
+ var getLanguagesMap = () => {
184
+ if (!languagesMap) {
185
+ languagesMap = Object.values(languages_exports).reduce((result, item) => {
186
+ item.aliases.forEach((alias) => {
187
+ result[alias] = item;
188
+ });
189
+ return result;
190
+ }, {});
191
+ }
192
+ return languagesMap;
193
+ };
194
+ var resolveLanguage = (info) => {
195
+ const alias = info.match(/^([^ :[{]+)/)?.[1] || "text";
196
+ return getLanguagesMap()[alias] ?? {
197
+ name: alias,
198
+ ext: alias,
199
+ aliases: [alias]
200
+ };
201
+ };
202
+
203
+ // src/plugins/codePlugin/resolveLineNumbers.ts
204
+ var resolveLineNumbers = (info) => {
205
+ if (/:line-numbers\b/.test(info)) {
206
+ return true;
207
+ }
208
+ if (/:no-line-numbers\b/.test(info)) {
209
+ return false;
210
+ }
211
+ return null;
212
+ };
213
+
214
+ // src/plugins/codePlugin/resolveVPre.ts
215
+ var resolveVPre = (info) => {
216
+ if (/:v-pre\b/.test(info)) {
217
+ return true;
218
+ }
219
+ if (/:no-v-pre\b/.test(info)) {
220
+ return false;
221
+ }
222
+ return null;
223
+ };
224
+
225
+ // src/plugins/codePlugin/codePlugin.ts
226
+ var codePlugin = (md, {
227
+ highlightLines = true,
228
+ lineNumbers = true,
229
+ preWrapper = true,
230
+ vPre: { block: vPreBlock = true, inline: vPreInline = true } = {}
231
+ } = {}) => {
232
+ md.renderer.rules.fence = (tokens, idx, options, env, slf) => {
233
+ const token = tokens[idx];
234
+ const info = token.info ? md.utils.unescapeAll(token.info).trim() : "";
235
+ const language = resolveLanguage(info);
236
+ const languageClass = `${options.langPrefix}${language.name}`;
237
+ const code = options.highlight?.(token.content, language.name, "") || md.utils.escapeHtml(token.content);
238
+ let result = code.startsWith("<pre") ? code : `<pre class="${languageClass}"><code>${code}</code></pre>`;
239
+ const useVPre = resolveVPre(info) ?? vPreBlock;
240
+ if (useVPre) {
241
+ result = `<pre v-pre${result.slice("<pre".length)}`;
242
+ }
243
+ if (!preWrapper) {
244
+ return result;
245
+ }
246
+ const lines = code.split("\n").slice(0, -1);
247
+ const highlightLinesRanges = highlightLines ? resolveHighlightLines(info) : null;
248
+ if (highlightLinesRanges) {
249
+ const highlightLinesCode = lines.map((_, index) => {
250
+ if (isHighlightLine(index + 1, highlightLinesRanges)) {
251
+ return '<div class="highlight-line">&nbsp;</div>';
252
+ }
253
+ return "<br>";
254
+ }).join("");
255
+ result = `${result}<div class="highlight-lines">${highlightLinesCode}</div>`;
256
+ }
257
+ const useLineNumbers = resolveLineNumbers(info) ?? (typeof lineNumbers === "number" ? lines.length >= lineNumbers : lineNumbers);
258
+ if (useLineNumbers) {
259
+ const lineNumbersCode = lines.map(() => `<div class="line-number"></div>`).join("");
260
+ result = `${result}<div class="line-numbers" aria-hidden="true">${lineNumbersCode}</div>`;
261
+ }
262
+ result = `<div class="${languageClass} ext-${language.ext}${useLineNumbers ? " line-numbers-mode" : ""}">${result}</div>`;
263
+ return result;
264
+ };
265
+ if (vPreInline) {
266
+ const rawInlineCodeRule = md.renderer.rules.code_inline;
267
+ md.renderer.rules.code_inline = (tokens, idx, options, env, slf) => {
268
+ const result = rawInlineCodeRule(tokens, idx, options, env, slf);
269
+ return `<code v-pre${result.slice("<code".length)}`;
270
+ };
271
+ }
272
+ };
273
+
274
+ // src/plugins/emojiPlugin.ts
275
+ import emojiPlugin from "markdown-it-emoji";
276
+
277
+ // src/plugins/importCodePlugin/createImportCodeBlockRule.ts
278
+ import { path as path2 } from "@vuepress/utils";
279
+ var MIN_LENGTH = 9;
280
+ var START_CODES = [64, 91, 99, 111, 100, 101];
281
+ var SYNTAX_RE = /^@\[code(?:{(?:(\d+)?-(\d+)?)})?(?: ([^\]]+))?\]\(([^)]*)\)/;
282
+ var createImportCodeBlockRule = ({ handleImportPath = (str) => str }) => (state, startLine, endLine, silent) => {
283
+ if (state.sCount[startLine] - state.blkIndent >= 4) {
284
+ return false;
285
+ }
286
+ const pos = state.bMarks[startLine] + state.tShift[startLine];
287
+ const max = state.eMarks[startLine];
288
+ if (pos + MIN_LENGTH > max)
289
+ return false;
290
+ for (let i = 0; i < START_CODES.length; i += 1) {
291
+ if (state.src.charCodeAt(pos + i) !== START_CODES[i]) {
292
+ return false;
293
+ }
294
+ }
295
+ const match = state.src.slice(pos, max).match(SYNTAX_RE);
296
+ if (!match)
297
+ return false;
298
+ if (silent)
299
+ return true;
300
+ const [, lineStart, lineEnd, info, importPath] = match;
301
+ const meta = {
302
+ importPath: handleImportPath(importPath),
303
+ lineStart: lineStart ? Number.parseInt(lineStart, 10) : 0,
304
+ lineEnd: lineEnd ? Number.parseInt(lineEnd, 10) : void 0
305
+ };
306
+ const token = state.push("import_code", "code", 0);
307
+ token.info = info ?? path2.extname(meta.importPath).slice(1);
308
+ token.markup = "```";
309
+ token.map = [startLine, startLine + 1];
310
+ token.meta = meta;
311
+ state.line = startLine + 1;
312
+ return true;
313
+ };
314
+
315
+ // src/plugins/importCodePlugin/resolveImportCode.ts
316
+ import { fs, path as path3 } from "@vuepress/utils";
317
+ var resolveImportCode = ({ importPath, lineStart, lineEnd }, { filePath }) => {
318
+ let importFilePath = importPath;
319
+ if (!path3.isAbsolute(importPath)) {
320
+ if (!filePath) {
321
+ return {
322
+ importFilePath: null,
323
+ importCode: "Error when resolving path"
324
+ };
325
+ }
326
+ importFilePath = path3.resolve(filePath, "..", importPath);
327
+ }
328
+ if (!fs.existsSync(importFilePath)) {
329
+ return {
330
+ importFilePath,
331
+ importCode: "File not found"
332
+ };
333
+ }
334
+ const fileContent = fs.readFileSync(importFilePath).toString();
335
+ return {
336
+ importFilePath,
337
+ importCode: fileContent.split("\n").slice(lineStart ? lineStart - 1 : lineStart, lineEnd).join("\n").replace(/\n?$/, "\n")
338
+ };
339
+ };
340
+
341
+ // src/plugins/importCodePlugin/importCodePlugin.ts
342
+ var importCodePlugin = (md, options = {}) => {
343
+ md.block.ruler.before(
344
+ "fence",
345
+ "import_code",
346
+ createImportCodeBlockRule(options),
347
+ {
348
+ alt: ["paragraph", "reference", "blockquote", "list"]
349
+ }
350
+ );
351
+ md.renderer.rules.import_code = (tokens, idx, options2, env, slf) => {
352
+ const token = tokens[idx];
353
+ const { importFilePath, importCode } = resolveImportCode(token.meta, env);
354
+ token.content = importCode;
355
+ if (importFilePath) {
356
+ const importedFiles = env.importedFiles || (env.importedFiles = []);
357
+ importedFiles.push(importFilePath);
358
+ }
359
+ return md.renderer.rules.fence(tokens, idx, options2, env, slf);
360
+ };
361
+ };
362
+
363
+ // src/plugins/linksPlugin/linksPlugin.ts
364
+ import { isLinkExternal } from "@vuepress/shared";
365
+
366
+ // src/plugins/linksPlugin/resolvePaths.ts
367
+ import { removeLeadingSlash } from "@vuepress/shared";
368
+ import { path as path4 } from "@vuepress/utils";
369
+ var resolvePaths = (rawPath, base, filePathRelative) => {
370
+ let absolutePath;
371
+ let relativePath;
372
+ if (rawPath.startsWith("/")) {
373
+ if (rawPath.endsWith(".md")) {
374
+ absolutePath = path4.join(base, rawPath);
375
+ relativePath = removeLeadingSlash(rawPath);
376
+ } else {
377
+ absolutePath = rawPath;
378
+ relativePath = path4.relative(base, absolutePath);
379
+ }
380
+ } else {
381
+ if (filePathRelative) {
382
+ relativePath = path4.join(
383
+ path4.dirname(encodeURI(filePathRelative)),
384
+ rawPath
385
+ );
386
+ absolutePath = path4.join(base, relativePath);
387
+ } else {
388
+ relativePath = rawPath.replace(/^(?:\.\/)?(.*)$/, "$1");
389
+ absolutePath = relativePath;
390
+ }
391
+ }
392
+ return {
393
+ absolutePath,
394
+ relativePath
395
+ };
396
+ };
397
+
398
+ // src/plugins/linksPlugin/linksPlugin.ts
399
+ var linksPlugin = (md, options = {}) => {
400
+ const internalTag = options.internalTag || "RouterLink";
401
+ const externalAttrs = {
402
+ target: "_blank",
403
+ rel: "noopener noreferrer",
404
+ ...options.externalAttrs
405
+ };
406
+ let hasOpenInternalLink = false;
407
+ const handleLinkOpen = (tokens, idx, env) => {
408
+ const token = tokens[idx];
409
+ const hrefIndex = token.attrIndex("href");
410
+ if (hrefIndex < 0) {
411
+ return;
412
+ }
413
+ const hrefAttr = token.attrs[hrefIndex];
414
+ const hrefLink = hrefAttr[1];
415
+ const { base = "/", filePathRelative = null } = env;
416
+ if (isLinkExternal(hrefLink, base)) {
417
+ Object.entries(externalAttrs).forEach(
418
+ ([key, val]) => token.attrSet(key, val)
419
+ );
420
+ return;
421
+ }
422
+ const internalLinkMatch = hrefLink.match(
423
+ /^((?:.*)(?:\/|\.md|\.html))(#.*)?$/
424
+ );
425
+ if (!internalLinkMatch) {
426
+ return;
427
+ }
428
+ const rawPath = internalLinkMatch[1];
429
+ const rawHash = internalLinkMatch[2] || "";
430
+ const { relativePath, absolutePath } = resolvePaths(
431
+ rawPath,
432
+ base,
433
+ filePathRelative
434
+ );
435
+ const normalizedPath = absolutePath.replace(new RegExp(`^${base}`), "/").replace(/(^|\/)(README|index).md$/i, "$1").replace(/\.md$/, ".html");
436
+ if (internalTag === "RouterLink") {
437
+ token.tag = internalTag;
438
+ hrefAttr[0] = "to";
439
+ hasOpenInternalLink = true;
440
+ }
441
+ hrefAttr[1] = `${normalizedPath}${rawHash}`;
442
+ const links = env.links || (env.links = []);
443
+ links.push({
444
+ raw: hrefLink,
445
+ relative: relativePath,
446
+ absolute: absolutePath
447
+ });
448
+ };
449
+ md.renderer.rules.link_open = (tokens, idx, options2, env, self) => {
450
+ handleLinkOpen(tokens, idx, env);
451
+ return self.renderToken(tokens, idx, options2);
452
+ };
453
+ md.renderer.rules.link_close = (tokens, idx, options2, env, self) => {
454
+ if (hasOpenInternalLink) {
455
+ hasOpenInternalLink = false;
456
+ tokens[idx].tag = internalTag;
457
+ }
458
+ return self.renderToken(tokens, idx, options2);
459
+ };
460
+ };
461
+
462
+ // src/markdown.ts
463
+ var createMarkdown = ({
464
+ anchor,
465
+ assets,
466
+ code,
467
+ component,
468
+ emoji,
469
+ frontmatter,
470
+ headers,
471
+ title,
472
+ importCode,
473
+ links,
474
+ sfc,
475
+ slugify = defaultSlugify,
476
+ toc,
477
+ ...markdownItOptions
478
+ } = {}) => {
479
+ const md = MarkdownIt({
480
+ ...markdownItOptions,
481
+ html: true
482
+ });
483
+ if (anchor !== false) {
484
+ md.use(anchorPlugin, {
485
+ level: [1, 2, 3, 4, 5, 6],
486
+ slugify,
487
+ permalink: anchorPlugin.permalink.ariaHidden({
488
+ class: "header-anchor",
489
+ symbol: "#",
490
+ space: true,
491
+ placement: "before"
492
+ }),
493
+ ...anchor
494
+ });
495
+ }
496
+ if (assets !== false) {
497
+ md.use(assetsPlugin, assets);
498
+ }
499
+ if (code !== false) {
500
+ md.use(codePlugin, code);
501
+ }
502
+ if (component !== false) {
503
+ md.use(componentPlugin);
504
+ }
505
+ if (emoji !== false) {
506
+ md.use(emojiPlugin, emoji);
507
+ }
508
+ if (frontmatter !== false) {
509
+ md.use(frontmatterPlugin, {
510
+ ...frontmatter,
511
+ grayMatterOptions: {
512
+ excerpt: true,
513
+ excerpt_separator: "<!-- more -->",
514
+ ...frontmatter?.grayMatterOptions
515
+ }
516
+ });
517
+ }
518
+ if (headers !== false) {
519
+ md.use(headersPlugin, {
520
+ level: [2, 3],
521
+ slugify,
522
+ ...headers
523
+ });
524
+ }
525
+ if (importCode !== false) {
526
+ md.use(importCodePlugin, importCode);
527
+ }
528
+ if (links !== false) {
529
+ md.use(linksPlugin, links);
530
+ }
531
+ if (sfc !== false) {
532
+ md.use(sfcPlugin, sfc);
533
+ }
534
+ if (toc !== false) {
535
+ md.use(tocPlugin, {
536
+ level: [2, 3],
537
+ slugify,
538
+ linkTag: "router-link",
539
+ ...toc
540
+ });
541
+ }
542
+ if (title !== false) {
543
+ md.use(titlePlugin);
544
+ }
545
+ return md;
546
+ };
547
+ export {
548
+ anchorPlugin,
549
+ assetsPlugin,
550
+ codePlugin,
551
+ componentPlugin,
552
+ createMarkdown,
553
+ emojiPlugin,
554
+ frontmatterPlugin,
555
+ headersPlugin,
556
+ importCodePlugin,
557
+ linksPlugin,
558
+ sfcPlugin,
559
+ titlePlugin,
560
+ tocPlugin
561
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vuepress/markdown",
3
- "version": "2.0.0-beta.50-pre",
3
+ "version": "2.0.0-beta.51",
4
4
  "description": "Markdown package of VuePress",
5
5
  "keywords": [
6
6
  "vuepress",
@@ -24,21 +24,21 @@
24
24
  "main": "./dist/index.js",
25
25
  "types": "./dist/index.d.ts",
26
26
  "files": [
27
- "./dist"
27
+ "dist"
28
28
  ],
29
29
  "dependencies": {
30
- "@mdit-vue/plugin-component": "^0.9.0",
31
- "@mdit-vue/plugin-frontmatter": "^0.9.1",
32
- "@mdit-vue/plugin-headers": "^0.9.1",
33
- "@mdit-vue/plugin-sfc": "^0.9.1",
34
- "@mdit-vue/plugin-title": "^0.9.1",
35
- "@mdit-vue/plugin-toc": "^0.9.1",
36
- "@mdit-vue/shared": "^0.9.1",
37
- "@mdit-vue/types": "^0.9.0",
30
+ "@mdit-vue/plugin-component": "^0.10.0",
31
+ "@mdit-vue/plugin-frontmatter": "^0.10.0",
32
+ "@mdit-vue/plugin-headers": "^0.10.0",
33
+ "@mdit-vue/plugin-sfc": "^0.10.0",
34
+ "@mdit-vue/plugin-title": "^0.10.0",
35
+ "@mdit-vue/plugin-toc": "^0.10.0",
36
+ "@mdit-vue/shared": "^0.10.0",
37
+ "@mdit-vue/types": "^0.10.0",
38
38
  "@types/markdown-it": "^12.2.3",
39
39
  "@types/markdown-it-emoji": "^2.0.2",
40
- "@vuepress/shared": "2.0.0-beta.50-pre",
41
- "@vuepress/utils": "2.0.0-beta.50-pre",
40
+ "@vuepress/shared": "2.0.0-beta.51",
41
+ "@vuepress/utils": "2.0.0-beta.51",
42
42
  "markdown-it": "^13.0.1",
43
43
  "markdown-it-anchor": "^8.6.4",
44
44
  "markdown-it-emoji": "^2.0.2",