@docusaurus/utils 2.0.0-beta.fbdeefcac → 2.0.0-rc.1
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.
- package/lib/constants.d.ts +73 -0
- package/lib/constants.d.ts.map +1 -0
- package/lib/constants.js +78 -0
- package/lib/constants.js.map +1 -0
- package/lib/dataFileUtils.d.ts +60 -0
- package/lib/dataFileUtils.d.ts.map +1 -0
- package/lib/dataFileUtils.js +91 -0
- package/lib/dataFileUtils.js.map +1 -0
- package/lib/emitUtils.d.ts +32 -0
- package/lib/emitUtils.d.ts.map +1 -0
- package/lib/emitUtils.js +80 -0
- package/lib/emitUtils.js.map +1 -0
- package/lib/gitUtils.d.ts +66 -0
- package/lib/gitUtils.d.ts.map +1 -0
- package/lib/gitUtils.js +63 -0
- package/lib/gitUtils.js.map +1 -0
- package/lib/globUtils.d.ts +40 -0
- package/lib/globUtils.d.ts.map +1 -0
- package/lib/globUtils.js +71 -0
- package/lib/globUtils.js.map +1 -0
- package/lib/hashUtils.d.ts +6 -4
- package/lib/hashUtils.d.ts.map +1 -0
- package/lib/hashUtils.js +13 -10
- package/lib/hashUtils.js.map +1 -0
- package/lib/i18nUtils.d.ts +53 -0
- package/lib/i18nUtils.d.ts.map +1 -0
- package/lib/i18nUtils.js +70 -0
- package/lib/i18nUtils.js.map +1 -0
- package/lib/index.d.ts +16 -75
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +91 -395
- package/lib/index.js.map +1 -0
- package/lib/jsUtils.d.ts +28 -0
- package/lib/jsUtils.d.ts.map +1 -0
- package/lib/jsUtils.js +57 -0
- package/lib/jsUtils.js.map +1 -0
- package/lib/markdownLinks.d.ts +49 -5
- package/lib/markdownLinks.d.ts.map +1 -0
- package/lib/markdownLinks.js +57 -13
- package/lib/markdownLinks.js.map +1 -0
- package/lib/markdownUtils.d.ts +112 -0
- package/lib/markdownUtils.d.ts.map +1 -0
- package/lib/markdownUtils.js +271 -0
- package/lib/markdownUtils.js.map +1 -0
- package/lib/pathUtils.d.ts +45 -1
- package/lib/pathUtils.d.ts.map +1 -0
- package/lib/pathUtils.js +92 -12
- package/lib/pathUtils.js.map +1 -0
- package/lib/shellUtils.d.ts +8 -0
- package/lib/shellUtils.d.ts.map +1 -0
- package/lib/shellUtils.js +21 -0
- package/lib/shellUtils.js.map +1 -0
- package/lib/slugger.d.ts +24 -0
- package/lib/slugger.d.ts.map +1 -0
- package/lib/slugger.js +23 -0
- package/lib/slugger.js.map +1 -0
- package/lib/tags.d.ts +59 -0
- package/lib/tags.d.ts.map +1 -0
- package/lib/tags.js +91 -0
- package/lib/tags.js.map +1 -0
- package/lib/urlUtils.d.ts +66 -0
- package/lib/urlUtils.d.ts.map +1 -0
- package/lib/urlUtils.js +207 -0
- package/lib/urlUtils.js.map +1 -0
- package/lib/webpackUtils.d.ts +35 -0
- package/lib/webpackUtils.d.ts.map +1 -0
- package/lib/webpackUtils.js +115 -0
- package/lib/webpackUtils.js.map +1 -0
- package/package.json +28 -10
- package/src/constants.ts +98 -0
- package/src/dataFileUtils.ts +122 -0
- package/src/deps.d.ts +10 -0
- package/src/emitUtils.ts +99 -0
- package/src/gitUtils.ts +146 -0
- package/src/globUtils.ts +85 -0
- package/src/hashUtils.ts +9 -8
- package/src/i18nUtils.ts +114 -0
- package/src/index.ts +97 -502
- package/src/jsUtils.ts +59 -0
- package/src/markdownLinks.ts +101 -30
- package/src/markdownUtils.ts +357 -0
- package/src/pathUtils.ts +93 -12
- package/src/shellUtils.ts +18 -0
- package/src/slugger.ts +36 -0
- package/src/tags.ts +130 -0
- package/src/urlUtils.ts +234 -0
- package/src/webpackUtils.ts +153 -0
- package/lib/.tsbuildinfo +0 -1
- package/lib/codeTranslationsUtils.d.ts +0 -11
- package/lib/codeTranslationsUtils.js +0 -50
- package/lib/escapePath.d.ts +0 -17
- package/lib/escapePath.js +0 -25
- package/lib/markdownParser.d.ts +0 -30
- package/lib/markdownParser.js +0 -140
- package/lib/posixPath.d.ts +0 -14
- package/lib/posixPath.js +0 -28
- package/src/__tests__/__fixtures__/defaultCodeTranslations/en.json +0 -4
- package/src/__tests__/__fixtures__/defaultCodeTranslations/fr-FR.json +0 -5
- package/src/__tests__/__fixtures__/defaultCodeTranslations/fr.json +0 -4
- package/src/__tests__/__snapshots__/index.test.ts.snap +0 -8
- package/src/__tests__/codeTranslationsUtils.test.ts +0 -112
- package/src/__tests__/escapePath.test.ts +0 -25
- package/src/__tests__/hashUtils.test.ts +0 -51
- package/src/__tests__/index.test.ts +0 -631
- package/src/__tests__/markdownParser.test.ts +0 -817
- package/src/__tests__/pathUtils.test.ts +0 -63
- package/src/__tests__/posixPath.test.ts +0 -25
- package/src/codeTranslationsUtils.ts +0 -56
- package/src/escapePath.ts +0 -23
- package/src/markdownParser.ts +0 -182
- package/src/posixPath.ts +0 -27
- package/tsconfig.json +0 -9
package/src/markdownLinks.ts
CHANGED
|
@@ -5,49 +5,96 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import
|
|
9
|
-
import {
|
|
8
|
+
import path from 'path';
|
|
9
|
+
import {getContentPathList} from './dataFileUtils';
|
|
10
|
+
import {aliasedSitePath} from './pathUtils';
|
|
10
11
|
|
|
12
|
+
/**
|
|
13
|
+
* Content plugins have a base path and a localized path to source content from.
|
|
14
|
+
* We will look into the localized path in priority.
|
|
15
|
+
*/
|
|
11
16
|
export type ContentPaths = {
|
|
17
|
+
/**
|
|
18
|
+
* The absolute path to the base content directory, like `"<siteDir>/docs"`.
|
|
19
|
+
*/
|
|
12
20
|
contentPath: string;
|
|
21
|
+
/**
|
|
22
|
+
* The absolute path to the localized content directory, like
|
|
23
|
+
* `"<siteDir>/i18n/zh-Hans/plugin-content-docs"`.
|
|
24
|
+
*/
|
|
13
25
|
contentPathLocalized: string;
|
|
14
26
|
};
|
|
15
27
|
|
|
28
|
+
/** Data structure representing each broken Markdown link to be reported. */
|
|
16
29
|
export type BrokenMarkdownLink<T extends ContentPaths> = {
|
|
30
|
+
/** Absolute path to the file containing this link. */
|
|
17
31
|
filePath: string;
|
|
32
|
+
/**
|
|
33
|
+
* This is generic because it may contain extra metadata like version name,
|
|
34
|
+
* which the reporter can provide for context.
|
|
35
|
+
*/
|
|
18
36
|
contentPaths: T;
|
|
37
|
+
/**
|
|
38
|
+
* The content of the link, like `"./brokenFile.md"`
|
|
39
|
+
*/
|
|
19
40
|
link: string;
|
|
20
41
|
};
|
|
21
42
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
};
|
|
34
|
-
|
|
43
|
+
/**
|
|
44
|
+
* Takes a Markdown file and replaces relative file references with their URL
|
|
45
|
+
* counterparts, e.g. `[link](./intro.md)` => `[link](/docs/intro)`, preserving
|
|
46
|
+
* everything else.
|
|
47
|
+
*
|
|
48
|
+
* This method uses best effort to find a matching file. The file reference can
|
|
49
|
+
* be relative to the directory of the current file (most likely) or any of the
|
|
50
|
+
* content paths (so `/tutorials/intro.md` can be resolved as
|
|
51
|
+
* `<siteDir>/docs/tutorials/intro.md`). Links that contain the `http(s):` or
|
|
52
|
+
* `@site/` prefix will always be ignored.
|
|
53
|
+
*/
|
|
35
54
|
export function replaceMarkdownLinks<T extends ContentPaths>({
|
|
36
55
|
siteDir,
|
|
37
56
|
fileString,
|
|
38
57
|
filePath,
|
|
39
58
|
contentPaths,
|
|
40
59
|
sourceToPermalink,
|
|
41
|
-
}:
|
|
42
|
-
|
|
43
|
-
|
|
60
|
+
}: {
|
|
61
|
+
/** Absolute path to the site directory, used to resolve aliased paths. */
|
|
62
|
+
siteDir: string;
|
|
63
|
+
/** The Markdown file content to be processed. */
|
|
64
|
+
fileString: string;
|
|
65
|
+
/** Absolute path to the current file containing `fileString`. */
|
|
66
|
+
filePath: string;
|
|
67
|
+
/** The content paths which the file reference may live in. */
|
|
68
|
+
contentPaths: T;
|
|
69
|
+
/**
|
|
70
|
+
* A map from source paths to their URLs. Source paths are `@site` aliased.
|
|
71
|
+
*/
|
|
72
|
+
sourceToPermalink: {[aliasedPath: string]: string};
|
|
73
|
+
}): {
|
|
74
|
+
/**
|
|
75
|
+
* The content with all Markdown file references replaced with their URLs.
|
|
76
|
+
* Unresolved links are left as-is.
|
|
77
|
+
*/
|
|
78
|
+
newContent: string;
|
|
79
|
+
/** The list of broken links, */
|
|
80
|
+
brokenMarkdownLinks: BrokenMarkdownLink<T>[];
|
|
81
|
+
} {
|
|
44
82
|
const brokenMarkdownLinks: BrokenMarkdownLink<T>[] = [];
|
|
45
83
|
|
|
46
84
|
// Replace internal markdown linking (except in fenced blocks).
|
|
47
85
|
let fencedBlock = false;
|
|
86
|
+
let lastCodeFence = '';
|
|
48
87
|
const lines = fileString.split('\n').map((line) => {
|
|
49
88
|
if (line.trim().startsWith('```')) {
|
|
50
|
-
|
|
89
|
+
const codeFence = line.trim().match(/^`+/)![0]!;
|
|
90
|
+
if (!fencedBlock) {
|
|
91
|
+
fencedBlock = true;
|
|
92
|
+
lastCodeFence = codeFence;
|
|
93
|
+
// If we are in a ````-fenced block, all ``` would be plain text instead
|
|
94
|
+
// of fences
|
|
95
|
+
} else if (codeFence.length >= lastCodeFence.length) {
|
|
96
|
+
fencedBlock = false;
|
|
97
|
+
}
|
|
51
98
|
}
|
|
52
99
|
if (fencedBlock) {
|
|
53
100
|
return line;
|
|
@@ -55,24 +102,48 @@ export function replaceMarkdownLinks<T extends ContentPaths>({
|
|
|
55
102
|
|
|
56
103
|
let modifiedLine = line;
|
|
57
104
|
// Replace inline-style links or reference-style links e.g:
|
|
58
|
-
// This is [Document 1](doc1.md)
|
|
59
|
-
// [doc1]: doc1.md
|
|
60
|
-
const mdRegex =
|
|
105
|
+
// This is [Document 1](doc1.md)
|
|
106
|
+
// [doc1]: doc1.md
|
|
107
|
+
const mdRegex =
|
|
108
|
+
/(?:\]\(|\]:\s*)(?!https?:\/\/|@site\/)(?<filename>[^'")\]\s>]+\.mdx?)/g;
|
|
61
109
|
let mdMatch = mdRegex.exec(modifiedLine);
|
|
62
110
|
while (mdMatch !== null) {
|
|
63
111
|
// Replace it to correct html link.
|
|
64
|
-
const mdLink = mdMatch
|
|
112
|
+
const mdLink = mdMatch.groups!.filename!;
|
|
113
|
+
|
|
114
|
+
const sourcesToTry: string[] = [];
|
|
115
|
+
// ./file.md and ../file.md are always relative to the current file
|
|
116
|
+
if (!mdLink.startsWith('./') && !mdLink.startsWith('../')) {
|
|
117
|
+
sourcesToTry.push(...getContentPathList(contentPaths), siteDir);
|
|
118
|
+
}
|
|
119
|
+
// /file.md is always relative to the content path
|
|
120
|
+
if (!mdLink.startsWith('/')) {
|
|
121
|
+
sourcesToTry.push(path.dirname(filePath));
|
|
122
|
+
}
|
|
65
123
|
|
|
66
|
-
const
|
|
67
|
-
|
|
124
|
+
const aliasedSourceMatch = sourcesToTry
|
|
125
|
+
.map((p) => path.join(p, decodeURIComponent(mdLink)))
|
|
126
|
+
.map((source) => aliasedSitePath(source, siteDir))
|
|
127
|
+
.find((source) => sourceToPermalink[source]);
|
|
68
128
|
|
|
69
|
-
const permalink: string | undefined =
|
|
70
|
-
sourceToPermalink[
|
|
71
|
-
|
|
72
|
-
sourceToPermalink[aliasedSource(`${contentPath}/${mdLink}`)];
|
|
129
|
+
const permalink: string | undefined = aliasedSourceMatch
|
|
130
|
+
? sourceToPermalink[aliasedSourceMatch]
|
|
131
|
+
: undefined;
|
|
73
132
|
|
|
74
133
|
if (permalink) {
|
|
75
|
-
|
|
134
|
+
// MDX won't be happy if the permalink contains a space, we need to
|
|
135
|
+
// convert it to %20
|
|
136
|
+
const encodedPermalink = permalink
|
|
137
|
+
.split('/')
|
|
138
|
+
.map((part) => part.replace(/\s/g, '%20'))
|
|
139
|
+
.join('/');
|
|
140
|
+
modifiedLine = modifiedLine.replace(
|
|
141
|
+
mdMatch[0]!,
|
|
142
|
+
mdMatch[0]!.replace(mdLink, encodedPermalink),
|
|
143
|
+
);
|
|
144
|
+
// Adjust the lastIndex to avoid passing over the next link if the
|
|
145
|
+
// newly replaced URL is shorter.
|
|
146
|
+
mdRegex.lastIndex += encodedPermalink.length - mdLink.length;
|
|
76
147
|
} else {
|
|
77
148
|
const brokenMarkdownLink: BrokenMarkdownLink<T> = {
|
|
78
149
|
contentPaths,
|
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import logger from '@docusaurus/logger';
|
|
9
|
+
import matter from 'gray-matter';
|
|
10
|
+
import {createSlugger, type Slugger, type SluggerOptions} from './slugger';
|
|
11
|
+
|
|
12
|
+
// Some utilities for parsing Markdown content. These things are only used on
|
|
13
|
+
// server-side when we infer metadata like `title` and `description` from the
|
|
14
|
+
// content. Most parsing is still done in MDX through the mdx-loader.
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Parses custom ID from a heading. The ID can contain any characters except
|
|
18
|
+
* `{#` and `}`.
|
|
19
|
+
*
|
|
20
|
+
* @param heading e.g. `## Some heading {#some-heading}` where the last
|
|
21
|
+
* character must be `}` for the ID to be recognized
|
|
22
|
+
*/
|
|
23
|
+
export function parseMarkdownHeadingId(heading: string): {
|
|
24
|
+
/**
|
|
25
|
+
* The heading content sans the ID part, right-trimmed. e.g. `## Some heading`
|
|
26
|
+
*/
|
|
27
|
+
text: string;
|
|
28
|
+
/** The heading ID. e.g. `some-heading` */
|
|
29
|
+
id: string | undefined;
|
|
30
|
+
} {
|
|
31
|
+
const customHeadingIdRegex = /\s*\{#(?<id>(?:.(?!\{#|\}))*.)\}$/;
|
|
32
|
+
const matches = customHeadingIdRegex.exec(heading);
|
|
33
|
+
if (matches) {
|
|
34
|
+
return {
|
|
35
|
+
text: heading.replace(matches[0]!, ''),
|
|
36
|
+
id: matches.groups!.id!,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
return {text: heading, id: undefined};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// TODO: Find a better way to do so, possibly by compiling the Markdown content,
|
|
43
|
+
// stripping out HTML tags and obtaining the first line.
|
|
44
|
+
/**
|
|
45
|
+
* Creates an excerpt of a Markdown file. This function will:
|
|
46
|
+
*
|
|
47
|
+
* - Ignore h1 headings (setext or atx)
|
|
48
|
+
* - Ignore import/export
|
|
49
|
+
* - Ignore code blocks
|
|
50
|
+
*
|
|
51
|
+
* And for the first contentful line, it will strip away most Markdown
|
|
52
|
+
* syntax, including HTML tags, emphasis, links (keeping the text), etc.
|
|
53
|
+
*/
|
|
54
|
+
export function createExcerpt(fileString: string): string | undefined {
|
|
55
|
+
const fileLines = fileString
|
|
56
|
+
.trimStart()
|
|
57
|
+
// Remove Markdown alternate title
|
|
58
|
+
.replace(/^[^\n]*\n[=]+/g, '')
|
|
59
|
+
.split('\n');
|
|
60
|
+
let inCode = false;
|
|
61
|
+
let inImport = false;
|
|
62
|
+
let lastCodeFence = '';
|
|
63
|
+
|
|
64
|
+
for (const fileLine of fileLines) {
|
|
65
|
+
if (fileLine === '' && inImport) {
|
|
66
|
+
inImport = false;
|
|
67
|
+
}
|
|
68
|
+
// Skip empty line.
|
|
69
|
+
if (!fileLine.trim()) {
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Skip import/export declaration.
|
|
74
|
+
if ((/^(?:import|export)\s.*/.test(fileLine) || inImport) && !inCode) {
|
|
75
|
+
inImport = true;
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Skip code block line.
|
|
80
|
+
if (fileLine.trim().startsWith('```')) {
|
|
81
|
+
const codeFence = fileLine.trim().match(/^`+/)![0]!;
|
|
82
|
+
if (!inCode) {
|
|
83
|
+
inCode = true;
|
|
84
|
+
lastCodeFence = codeFence;
|
|
85
|
+
// If we are in a ````-fenced block, all ``` would be plain text instead
|
|
86
|
+
// of fences
|
|
87
|
+
} else if (codeFence.length >= lastCodeFence.length) {
|
|
88
|
+
inCode = false;
|
|
89
|
+
}
|
|
90
|
+
continue;
|
|
91
|
+
} else if (inCode) {
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const cleanedLine = fileLine
|
|
96
|
+
// Remove HTML tags.
|
|
97
|
+
.replace(/<[^>]*>/g, '')
|
|
98
|
+
// Remove Title headers
|
|
99
|
+
.replace(/^#[^#]+#?/gm, '')
|
|
100
|
+
// Remove Markdown + ATX-style headers
|
|
101
|
+
.replace(/^#{1,6}\s*(?<text>[^#]*)\s*#{0,6}/gm, '$1')
|
|
102
|
+
// Remove emphasis.
|
|
103
|
+
.replace(/(?<opening>[*_]{1,3})(?<text>.*?)\1/g, '$2')
|
|
104
|
+
// Remove strikethroughs.
|
|
105
|
+
.replace(/~~(?<text>\S.*\S)~~/g, '$1')
|
|
106
|
+
// Remove images.
|
|
107
|
+
.replace(/!\[(?<alt>.*?)\][[(].*?[\])]/g, '$1')
|
|
108
|
+
// Remove footnotes.
|
|
109
|
+
.replace(/\[\^.+?\](?:: .*$)?/g, '')
|
|
110
|
+
// Remove inline links.
|
|
111
|
+
.replace(/\[(?<alt>.*?)\][[(].*?[\])]/g, '$1')
|
|
112
|
+
// Remove inline code.
|
|
113
|
+
.replace(/`(?<text>.+?)`/g, '$1')
|
|
114
|
+
// Remove blockquotes.
|
|
115
|
+
.replace(/^\s{0,3}>\s?/g, '')
|
|
116
|
+
// Remove admonition definition.
|
|
117
|
+
.replace(/:::.*/, '')
|
|
118
|
+
// Remove Emoji names within colons include preceding whitespace.
|
|
119
|
+
.replace(/\s?:(?:::|[^:\n])+:/g, '')
|
|
120
|
+
// Remove custom Markdown heading id.
|
|
121
|
+
.replace(/\{#*[\w-]+\}/, '')
|
|
122
|
+
.trim();
|
|
123
|
+
|
|
124
|
+
if (cleanedLine) {
|
|
125
|
+
return cleanedLine;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return undefined;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Takes a raw Markdown file content, and parses the front matter using
|
|
134
|
+
* gray-matter. Worth noting that gray-matter accepts TOML and other markup
|
|
135
|
+
* languages as well.
|
|
136
|
+
*
|
|
137
|
+
* @throws Throws when gray-matter throws. e.g.:
|
|
138
|
+
* ```md
|
|
139
|
+
* ---
|
|
140
|
+
* foo: : bar
|
|
141
|
+
* ---
|
|
142
|
+
* ```
|
|
143
|
+
*/
|
|
144
|
+
export function parseFrontMatter(markdownFileContent: string): {
|
|
145
|
+
/** Front matter as parsed by gray-matter. */
|
|
146
|
+
frontMatter: {[key: string]: unknown};
|
|
147
|
+
/** The remaining content, trimmed. */
|
|
148
|
+
content: string;
|
|
149
|
+
} {
|
|
150
|
+
const {data, content} = matter(markdownFileContent);
|
|
151
|
+
return {
|
|
152
|
+
frontMatter: data,
|
|
153
|
+
content: content.trim(),
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function toTextContentTitle(contentTitle: string): string {
|
|
158
|
+
if (contentTitle.startsWith('`') && contentTitle.endsWith('`')) {
|
|
159
|
+
return contentTitle.substring(1, contentTitle.length - 1);
|
|
160
|
+
}
|
|
161
|
+
return contentTitle;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
type ParseMarkdownContentTitleOptions = {
|
|
165
|
+
/**
|
|
166
|
+
* If `true`, the matching title will be removed from the returned content.
|
|
167
|
+
* We can promise that at least one empty line will be left between the
|
|
168
|
+
* content before and after, but you shouldn't make too much assumption
|
|
169
|
+
* about what's left.
|
|
170
|
+
*/
|
|
171
|
+
removeContentTitle?: boolean;
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Takes the raw Markdown content, without front matter, and tries to find an h1
|
|
176
|
+
* title (setext or atx) to be used as metadata.
|
|
177
|
+
*
|
|
178
|
+
* It only searches until the first contentful paragraph, ignoring import/export
|
|
179
|
+
* declarations.
|
|
180
|
+
*
|
|
181
|
+
* It will try to convert markdown to reasonable text, but won't be best effort,
|
|
182
|
+
* since it's only used as a fallback when `frontMatter.title` is not provided.
|
|
183
|
+
* For now, we just unwrap inline code (``# `config.js` `` => `config.js`).
|
|
184
|
+
*/
|
|
185
|
+
export function parseMarkdownContentTitle(
|
|
186
|
+
contentUntrimmed: string,
|
|
187
|
+
options?: ParseMarkdownContentTitleOptions,
|
|
188
|
+
): {
|
|
189
|
+
/** The content, optionally without the content title. */
|
|
190
|
+
content: string;
|
|
191
|
+
/** The title, trimmed and without the `#`. */
|
|
192
|
+
contentTitle: string | undefined;
|
|
193
|
+
} {
|
|
194
|
+
const removeContentTitleOption = options?.removeContentTitle ?? false;
|
|
195
|
+
|
|
196
|
+
const content = contentUntrimmed.trim();
|
|
197
|
+
// We only need to detect import statements that will be parsed by MDX as
|
|
198
|
+
// `import` nodes, as broken syntax can't render anyways. That means any block
|
|
199
|
+
// that has `import` at the very beginning and surrounded by empty lines.
|
|
200
|
+
const contentWithoutImport = content
|
|
201
|
+
.replace(/^(?:import\s(?:.|\r?\n(?!\r?\n))*(?:\r?\n){2,})*/, '')
|
|
202
|
+
.trim();
|
|
203
|
+
|
|
204
|
+
const regularTitleMatch = /^#[ \t]+(?<title>[^ \t].*)(?:\r?\n|$)/.exec(
|
|
205
|
+
contentWithoutImport,
|
|
206
|
+
);
|
|
207
|
+
const alternateTitleMatch = /^(?<title>.*)\r?\n=+(?:\r?\n|$)/.exec(
|
|
208
|
+
contentWithoutImport,
|
|
209
|
+
);
|
|
210
|
+
|
|
211
|
+
const titleMatch = regularTitleMatch ?? alternateTitleMatch;
|
|
212
|
+
if (!titleMatch) {
|
|
213
|
+
return {content, contentTitle: undefined};
|
|
214
|
+
}
|
|
215
|
+
const newContent = removeContentTitleOption
|
|
216
|
+
? content.replace(titleMatch[0]!, '')
|
|
217
|
+
: content;
|
|
218
|
+
if (regularTitleMatch) {
|
|
219
|
+
return {
|
|
220
|
+
content: newContent.trim(),
|
|
221
|
+
contentTitle: toTextContentTitle(
|
|
222
|
+
regularTitleMatch
|
|
223
|
+
.groups!.title!.trim()
|
|
224
|
+
.replace(/\s*(?:\{#*[\w-]+\}|#+)$/, ''),
|
|
225
|
+
).trim(),
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
return {
|
|
229
|
+
content: newContent.trim(),
|
|
230
|
+
contentTitle: toTextContentTitle(
|
|
231
|
+
alternateTitleMatch!.groups!.title!.trim().replace(/\s*=+$/, ''),
|
|
232
|
+
).trim(),
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Makes a full-round parse.
|
|
238
|
+
*
|
|
239
|
+
* @throws Throws when `parseFrontMatter` throws, usually because of invalid
|
|
240
|
+
* syntax.
|
|
241
|
+
*/
|
|
242
|
+
export function parseMarkdownString(
|
|
243
|
+
markdownFileContent: string,
|
|
244
|
+
options?: ParseMarkdownContentTitleOptions,
|
|
245
|
+
): {
|
|
246
|
+
/** @see {@link parseFrontMatter} */
|
|
247
|
+
frontMatter: {[key: string]: unknown};
|
|
248
|
+
/** @see {@link parseMarkdownContentTitle} */
|
|
249
|
+
contentTitle: string | undefined;
|
|
250
|
+
/** @see {@link createExcerpt} */
|
|
251
|
+
excerpt: string | undefined;
|
|
252
|
+
/**
|
|
253
|
+
* Content without front matter and (optionally) without title, depending on
|
|
254
|
+
* the `removeContentTitle` option.
|
|
255
|
+
*/
|
|
256
|
+
content: string;
|
|
257
|
+
} {
|
|
258
|
+
try {
|
|
259
|
+
const {frontMatter, content: contentWithoutFrontMatter} =
|
|
260
|
+
parseFrontMatter(markdownFileContent);
|
|
261
|
+
|
|
262
|
+
const {content, contentTitle} = parseMarkdownContentTitle(
|
|
263
|
+
contentWithoutFrontMatter,
|
|
264
|
+
options,
|
|
265
|
+
);
|
|
266
|
+
|
|
267
|
+
const excerpt = createExcerpt(content);
|
|
268
|
+
|
|
269
|
+
return {
|
|
270
|
+
frontMatter,
|
|
271
|
+
content,
|
|
272
|
+
contentTitle,
|
|
273
|
+
excerpt,
|
|
274
|
+
};
|
|
275
|
+
} catch (err) {
|
|
276
|
+
logger.error(`Error while parsing Markdown front matter.
|
|
277
|
+
This can happen if you use special characters in front matter values (try using double quotes around that value).`);
|
|
278
|
+
throw err;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function unwrapMarkdownLinks(line: string): string {
|
|
283
|
+
return line.replace(
|
|
284
|
+
/\[(?<alt>[^\]]+)\]\([^)]+\)/g,
|
|
285
|
+
(match, p1: string) => p1,
|
|
286
|
+
);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function addHeadingId(
|
|
290
|
+
line: string,
|
|
291
|
+
slugger: Slugger,
|
|
292
|
+
maintainCase: boolean,
|
|
293
|
+
): string {
|
|
294
|
+
let headingLevel = 0;
|
|
295
|
+
while (line.charAt(headingLevel) === '#') {
|
|
296
|
+
headingLevel += 1;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
const headingText = line.slice(headingLevel).trimEnd();
|
|
300
|
+
const headingHashes = line.slice(0, headingLevel);
|
|
301
|
+
const slug = slugger.slug(unwrapMarkdownLinks(headingText).trim(), {
|
|
302
|
+
maintainCase,
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
return `${headingHashes}${headingText} {#${slug}}`;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
export type WriteHeadingIDOptions = SluggerOptions & {
|
|
309
|
+
/** Overwrite existing heading IDs. */
|
|
310
|
+
overwrite?: boolean;
|
|
311
|
+
};
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Takes Markdown content, returns new content with heading IDs written.
|
|
315
|
+
* Respects existing IDs (unless `overwrite=true`) and never generates colliding
|
|
316
|
+
* IDs (through the slugger).
|
|
317
|
+
*/
|
|
318
|
+
export function writeMarkdownHeadingId(
|
|
319
|
+
content: string,
|
|
320
|
+
options: WriteHeadingIDOptions = {maintainCase: false, overwrite: false},
|
|
321
|
+
): string {
|
|
322
|
+
const {maintainCase = false, overwrite = false} = options;
|
|
323
|
+
const lines = content.split('\n');
|
|
324
|
+
const slugger = createSlugger();
|
|
325
|
+
|
|
326
|
+
// If we can't overwrite existing slugs, make sure other headings don't
|
|
327
|
+
// generate colliding slugs by first marking these slugs as occupied
|
|
328
|
+
if (!overwrite) {
|
|
329
|
+
lines.forEach((line) => {
|
|
330
|
+
const parsedHeading = parseMarkdownHeadingId(line);
|
|
331
|
+
if (parsedHeading.id) {
|
|
332
|
+
slugger.slug(parsedHeading.id);
|
|
333
|
+
}
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
let inCode = false;
|
|
338
|
+
return lines
|
|
339
|
+
.map((line) => {
|
|
340
|
+
if (line.startsWith('```')) {
|
|
341
|
+
inCode = !inCode;
|
|
342
|
+
return line;
|
|
343
|
+
}
|
|
344
|
+
// Ignore h1 headings, as we don't create anchor links for those
|
|
345
|
+
if (inCode || !line.startsWith('##')) {
|
|
346
|
+
return line;
|
|
347
|
+
}
|
|
348
|
+
const parsedHeading = parseMarkdownHeadingId(line);
|
|
349
|
+
|
|
350
|
+
// Do not process if id is already there
|
|
351
|
+
if (parsedHeading.id && !overwrite) {
|
|
352
|
+
return line;
|
|
353
|
+
}
|
|
354
|
+
return addHeadingId(parsedHeading.text, slugger, maintainCase);
|
|
355
|
+
})
|
|
356
|
+
.join('\n');
|
|
357
|
+
}
|
package/src/pathUtils.ts
CHANGED
|
@@ -5,25 +5,29 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
import path from 'path';
|
|
9
9
|
|
|
10
|
-
//
|
|
10
|
+
// Based on https://github.com/gatsbyjs/gatsby/pull/21518/files
|
|
11
|
+
// macOS (APFS) and Windows (NTFS) filename length limit = 255 chars,
|
|
12
|
+
// Others = 255 bytes
|
|
11
13
|
const MAX_PATH_SEGMENT_CHARS = 255;
|
|
12
14
|
const MAX_PATH_SEGMENT_BYTES = 255;
|
|
13
15
|
// Space for appending things to the string like file extensions and so on
|
|
14
16
|
const SPACE_FOR_APPENDING = 10;
|
|
15
17
|
|
|
16
|
-
const isMacOs = process.platform ===
|
|
17
|
-
const isWindows = process.platform ===
|
|
18
|
+
const isMacOs = () => process.platform === 'darwin';
|
|
19
|
+
const isWindows = () => process.platform === 'win32';
|
|
18
20
|
|
|
19
|
-
export const isNameTooLong = (str: string): boolean =>
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
export const isNameTooLong = (str: string): boolean =>
|
|
22
|
+
// Not entirely correct: we can't assume FS from OS. But good enough?
|
|
23
|
+
isMacOs() || isWindows()
|
|
24
|
+
? // Windows (NTFS) and macOS (APFS) filename length limit (255 chars)
|
|
25
|
+
str.length + SPACE_FOR_APPENDING > MAX_PATH_SEGMENT_CHARS
|
|
26
|
+
: // Other (255 bytes)
|
|
27
|
+
Buffer.from(str).length + SPACE_FOR_APPENDING > MAX_PATH_SEGMENT_BYTES;
|
|
24
28
|
|
|
25
|
-
export
|
|
26
|
-
if (isMacOs || isWindows) {
|
|
29
|
+
export function shortName(str: string): string {
|
|
30
|
+
if (isMacOs() || isWindows()) {
|
|
27
31
|
const overflowingChars = str.length - MAX_PATH_SEGMENT_CHARS;
|
|
28
32
|
return str.slice(
|
|
29
33
|
0,
|
|
@@ -39,4 +43,81 @@ export const shortName = (str: string): string => {
|
|
|
39
43
|
Buffer.byteLength(strBuffer) - overflowingBytes - SPACE_FOR_APPENDING - 1,
|
|
40
44
|
)
|
|
41
45
|
.toString();
|
|
42
|
-
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Convert Windows backslash paths to posix style paths.
|
|
50
|
+
* E.g: endi\lie -> endi/lie
|
|
51
|
+
*
|
|
52
|
+
* Returns original path if the posix counterpart is not valid Windows path.
|
|
53
|
+
* This makes the legacy code that uses posixPath safe; but also makes it less
|
|
54
|
+
* useful when you actually want a path with forward slashes (e.g. for URL)
|
|
55
|
+
*
|
|
56
|
+
* Adopted from https://github.com/sindresorhus/slash/blob/main/index.js
|
|
57
|
+
*/
|
|
58
|
+
export function posixPath(str: string): string {
|
|
59
|
+
const isExtendedLengthPath = str.startsWith('\\\\?\\');
|
|
60
|
+
|
|
61
|
+
// Forward slashes are only valid Windows paths when they don't contain non-
|
|
62
|
+
// ascii characters.
|
|
63
|
+
// eslint-disable-next-line no-control-regex
|
|
64
|
+
const hasNonAscii = /[^\u0000-\u0080]+/.test(str);
|
|
65
|
+
|
|
66
|
+
if (isExtendedLengthPath || hasNonAscii) {
|
|
67
|
+
return str;
|
|
68
|
+
}
|
|
69
|
+
return str.replace(/\\/g, '/');
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* When you want to display a path in a message/warning/error, it's more
|
|
74
|
+
* convenient to:
|
|
75
|
+
*
|
|
76
|
+
* - make it relative to `cwd()`
|
|
77
|
+
* - convert to posix (ie not using windows \ path separator)
|
|
78
|
+
*
|
|
79
|
+
* This way, Jest tests can run more reliably on any computer/CI on both
|
|
80
|
+
* Unix/Windows
|
|
81
|
+
* For Windows users this is not perfect (as they see / instead of \) but it's
|
|
82
|
+
* probably good enough
|
|
83
|
+
*/
|
|
84
|
+
export function toMessageRelativeFilePath(filePath: string): string {
|
|
85
|
+
return posixPath(path.relative(process.cwd(), filePath));
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Alias filepath relative to site directory, very useful so that we
|
|
90
|
+
* don't expose user's site structure.
|
|
91
|
+
* Example: some/path/to/website/docs/foo.md -> @site/docs/foo.md
|
|
92
|
+
*/
|
|
93
|
+
export function aliasedSitePath(filePath: string, siteDir: string): string {
|
|
94
|
+
const relativePath = posixPath(path.relative(siteDir, filePath));
|
|
95
|
+
// Cannot use path.join() as it resolves '../' and removes
|
|
96
|
+
// the '@site'. Let webpack loader resolve it.
|
|
97
|
+
return `@site/${relativePath}`;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* When you have a path like C:\X\Y
|
|
102
|
+
* It is not safe to use directly when generating code
|
|
103
|
+
* For example, this would fail due to unescaped \:
|
|
104
|
+
* `<img src={require('${filePath}')} />`
|
|
105
|
+
* But this would work: `<img src={require('${escapePath(filePath)}')} />`
|
|
106
|
+
*
|
|
107
|
+
* posixPath can't be used in all cases, because forward slashes are only valid
|
|
108
|
+
* Windows paths when they don't contain non-ascii characters, and posixPath
|
|
109
|
+
* doesn't escape those that fail to be converted.
|
|
110
|
+
*/
|
|
111
|
+
export function escapePath(str: string): string {
|
|
112
|
+
const escaped = JSON.stringify(str);
|
|
113
|
+
|
|
114
|
+
// Remove the " around the json string;
|
|
115
|
+
return escaped.substring(1, escaped.length - 1);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export function addTrailingPathSeparator(str: string): string {
|
|
119
|
+
return str.endsWith(path.sep)
|
|
120
|
+
? str
|
|
121
|
+
: // If this is Windows, we need to change the forward slash to backward
|
|
122
|
+
`${str.replace(/\/$/, '')}${path.sep}`;
|
|
123
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
// TODO move from shelljs to execa later?
|
|
9
|
+
// Execa is well maintained and widely used
|
|
10
|
+
// Even shelljs recommends execa for security / escaping:
|
|
11
|
+
// https://github.com/shelljs/shelljs/wiki/Security-guidelines
|
|
12
|
+
|
|
13
|
+
// Inspired by https://github.com/xxorax/node-shell-escape/blob/master/shell-escape.js
|
|
14
|
+
export function escapeShellArg(s: string): string {
|
|
15
|
+
let res = `'${s.replace(/'/g, "'\\''")}'`;
|
|
16
|
+
res = res.replace(/^(?:'')+/g, '').replace(/\\'''/g, "\\'");
|
|
17
|
+
return res;
|
|
18
|
+
}
|