@docusaurus/utils 2.0.0-beta.208c08983 → 2.0.0-beta.21

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 (110) hide show
  1. package/lib/constants.d.ts +73 -0
  2. package/lib/constants.d.ts.map +1 -0
  3. package/lib/constants.js +78 -0
  4. package/lib/constants.js.map +1 -0
  5. package/lib/dataFileUtils.d.ts +60 -0
  6. package/lib/dataFileUtils.d.ts.map +1 -0
  7. package/lib/dataFileUtils.js +91 -0
  8. package/lib/dataFileUtils.js.map +1 -0
  9. package/lib/emitUtils.d.ts +32 -0
  10. package/lib/emitUtils.d.ts.map +1 -0
  11. package/lib/emitUtils.js +80 -0
  12. package/lib/emitUtils.js.map +1 -0
  13. package/lib/gitUtils.d.ts +66 -0
  14. package/lib/gitUtils.d.ts.map +1 -0
  15. package/lib/gitUtils.js +63 -0
  16. package/lib/gitUtils.js.map +1 -0
  17. package/lib/globUtils.d.ts +40 -0
  18. package/lib/globUtils.d.ts.map +1 -0
  19. package/lib/globUtils.js +71 -0
  20. package/lib/globUtils.js.map +1 -0
  21. package/lib/hashUtils.d.ts +17 -0
  22. package/lib/hashUtils.d.ts.map +1 -0
  23. package/lib/hashUtils.js +42 -0
  24. package/lib/hashUtils.js.map +1 -0
  25. package/lib/i18nUtils.d.ts +51 -0
  26. package/lib/i18nUtils.d.ts.map +1 -0
  27. package/lib/i18nUtils.js +69 -0
  28. package/lib/i18nUtils.js.map +1 -0
  29. package/lib/index.d.ts +17 -82
  30. package/lib/index.d.ts.map +1 -0
  31. package/lib/index.js +92 -412
  32. package/lib/index.js.map +1 -0
  33. package/lib/jsUtils.d.ts +45 -0
  34. package/lib/jsUtils.d.ts.map +1 -0
  35. package/lib/jsUtils.js +94 -0
  36. package/lib/jsUtils.js.map +1 -0
  37. package/lib/markdownLinks.d.ts +49 -5
  38. package/lib/markdownLinks.d.ts.map +1 -0
  39. package/lib/markdownLinks.js +57 -13
  40. package/lib/markdownLinks.js.map +1 -0
  41. package/lib/markdownUtils.d.ts +112 -0
  42. package/lib/markdownUtils.d.ts.map +1 -0
  43. package/lib/markdownUtils.js +271 -0
  44. package/lib/markdownUtils.js.map +1 -0
  45. package/lib/pathUtils.d.ts +52 -0
  46. package/lib/pathUtils.d.ts.map +1 -0
  47. package/lib/pathUtils.js +115 -0
  48. package/lib/pathUtils.js.map +1 -0
  49. package/lib/shellUtils.d.ts +8 -0
  50. package/lib/shellUtils.d.ts.map +1 -0
  51. package/lib/shellUtils.js +21 -0
  52. package/lib/shellUtils.js.map +1 -0
  53. package/lib/slugger.d.ts +24 -0
  54. package/lib/slugger.d.ts.map +1 -0
  55. package/lib/slugger.js +23 -0
  56. package/lib/slugger.js.map +1 -0
  57. package/lib/tags.d.ts +59 -0
  58. package/lib/tags.d.ts.map +1 -0
  59. package/lib/tags.js +91 -0
  60. package/lib/tags.js.map +1 -0
  61. package/lib/urlUtils.d.ts +66 -0
  62. package/lib/urlUtils.d.ts.map +1 -0
  63. package/lib/urlUtils.js +207 -0
  64. package/lib/urlUtils.js.map +1 -0
  65. package/lib/webpackUtils.d.ts +35 -0
  66. package/lib/webpackUtils.d.ts.map +1 -0
  67. package/lib/webpackUtils.js +115 -0
  68. package/lib/webpackUtils.js.map +1 -0
  69. package/package.json +20 -10
  70. package/src/constants.ts +98 -0
  71. package/src/dataFileUtils.ts +122 -0
  72. package/src/deps.d.ts +10 -0
  73. package/src/emitUtils.ts +99 -0
  74. package/src/gitUtils.ts +146 -0
  75. package/src/globUtils.ts +85 -0
  76. package/src/hashUtils.ts +38 -0
  77. package/src/i18nUtils.ts +115 -0
  78. package/src/index.ts +99 -523
  79. package/src/jsUtils.ts +102 -0
  80. package/src/markdownLinks.ts +101 -30
  81. package/src/markdownUtils.ts +357 -0
  82. package/src/pathUtils.ts +123 -0
  83. package/src/shellUtils.ts +18 -0
  84. package/src/slugger.ts +36 -0
  85. package/src/tags.ts +130 -0
  86. package/src/urlUtils.ts +234 -0
  87. package/src/webpackUtils.ts +153 -0
  88. package/lib/.tsbuildinfo +0 -1
  89. package/lib/codeTranslationsUtils.d.ts +0 -11
  90. package/lib/codeTranslationsUtils.js +0 -50
  91. package/lib/escapePath.d.ts +0 -17
  92. package/lib/escapePath.js +0 -25
  93. package/lib/markdownParser.d.ts +0 -30
  94. package/lib/markdownParser.js +0 -141
  95. package/lib/posixPath.d.ts +0 -14
  96. package/lib/posixPath.js +0 -28
  97. package/src/__tests__/__fixtures__/defaultCodeTranslations/en.json +0 -4
  98. package/src/__tests__/__fixtures__/defaultCodeTranslations/fr-FR.json +0 -5
  99. package/src/__tests__/__fixtures__/defaultCodeTranslations/fr.json +0 -4
  100. package/src/__tests__/__snapshots__/index.test.ts.snap +0 -8
  101. package/src/__tests__/codeTranslationsUtils.test.ts +0 -112
  102. package/src/__tests__/escapePath.test.ts +0 -25
  103. package/src/__tests__/index.test.ts +0 -681
  104. package/src/__tests__/markdownParser.test.ts +0 -817
  105. package/src/__tests__/posixPath.test.ts +0 -25
  106. package/src/codeTranslationsUtils.ts +0 -56
  107. package/src/escapePath.ts +0 -23
  108. package/src/markdownParser.ts +0 -183
  109. package/src/posixPath.ts +0 -27
  110. package/tsconfig.json +0 -9
package/src/jsUtils.ts ADDED
@@ -0,0 +1,102 @@
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 type {ReportingSeverity} from '@docusaurus/types';
10
+
11
+ /** Removes a given string suffix from `str`. */
12
+ export function removeSuffix(str: string, suffix: string): string {
13
+ if (suffix === '') {
14
+ // str.slice(0, 0) is ""
15
+ return str;
16
+ }
17
+ return str.endsWith(suffix) ? str.slice(0, -suffix.length) : str;
18
+ }
19
+
20
+ /** Removes a given string prefix from `str`. */
21
+ export function removePrefix(str: string, prefix: string): string {
22
+ return str.startsWith(prefix) ? str.slice(prefix.length) : str;
23
+ }
24
+
25
+ /**
26
+ * `Array#map` for async operations where order matters.
27
+ * @param array The array to traverse.
28
+ * @param action An async action to be performed on every array item. Will be
29
+ * awaited before working on the next.
30
+ * @returns The list of results returned from every `action(item)`
31
+ */
32
+ export async function mapAsyncSequential<T, R>(
33
+ array: T[],
34
+ action: (t: T) => Promise<R>,
35
+ ): Promise<R[]> {
36
+ const results: R[] = [];
37
+ for (const t of array) {
38
+ const result = await action(t);
39
+ results.push(result);
40
+ }
41
+ return results;
42
+ }
43
+
44
+ /**
45
+ * `Array#find` for async operations where order matters.
46
+ * @param array The array to traverse.
47
+ * @param predicate An async predicate to be called on every array item. Should
48
+ * return a boolean indicating whether the currently element should be returned.
49
+ * @returns The function immediately returns the first item on which `predicate`
50
+ * returns `true`, or `undefined` if none matches the predicate.
51
+ */
52
+ export async function findAsyncSequential<T>(
53
+ array: T[],
54
+ predicate: (t: T) => Promise<boolean>,
55
+ ): Promise<T | undefined> {
56
+ for (const t of array) {
57
+ if (await predicate(t)) {
58
+ return t;
59
+ }
60
+ }
61
+ return undefined;
62
+ }
63
+
64
+ /**
65
+ * Takes a message and reports it according to the severity that the user wants.
66
+ *
67
+ * - `ignore`: completely no-op
68
+ * - `log`: uses the `INFO` log level
69
+ * - `warn`: uses the `WARN` log level
70
+ * - `error`: uses the `ERROR` log level
71
+ * - `throw`: aborts the process, throws the error.
72
+ *
73
+ * Since the logger doesn't have logging level filters yet, these severities
74
+ * mostly just differ by their colors.
75
+ *
76
+ * @throws In addition to throwing when `reportingSeverity === "throw"`, this
77
+ * function also throws if `reportingSeverity` is not one of the above.
78
+ */
79
+ export function reportMessage(
80
+ message: string,
81
+ reportingSeverity: ReportingSeverity,
82
+ ): void {
83
+ switch (reportingSeverity) {
84
+ case 'ignore':
85
+ break;
86
+ case 'log':
87
+ logger.info(message);
88
+ break;
89
+ case 'warn':
90
+ logger.warn(message);
91
+ break;
92
+ case 'error':
93
+ logger.error(message);
94
+ break;
95
+ case 'throw':
96
+ throw new Error(message);
97
+ default:
98
+ throw new Error(
99
+ `Unexpected "reportingSeverity" value: ${reportingSeverity}.`,
100
+ );
101
+ }
102
+ }
@@ -5,49 +5,96 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
- import {resolve} from 'url';
9
- import {aliasedSitePath} from './index';
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
- export type ReplaceMarkdownLinksParams<T extends ContentPaths> = {
23
- siteDir: string;
24
- fileString: string;
25
- filePath: string;
26
- contentPaths: T;
27
- sourceToPermalink: Record<string, string>;
28
- };
29
-
30
- export type ReplaceMarkdownLinksReturn<T extends ContentPaths> = {
31
- newContent: string;
32
- brokenMarkdownLinks: BrokenMarkdownLink<T>[];
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
- }: ReplaceMarkdownLinksParams<T>): ReplaceMarkdownLinksReturn<T> {
42
- const {contentPath, contentPathLocalized} = contentPaths;
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
- fencedBlock = !fencedBlock;
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) -> we replace this doc1.md with correct link
59
- // [doc1]: doc1.md -> we replace this doc1.md with correct link
60
- const mdRegex = /(?:(?:\]\()|(?:\]:\s?))(?!https)([^'")\]\s>]+\.mdx?)/g;
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[1];
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 aliasedSource = (source: string) =>
67
- aliasedSitePath(source, siteDir);
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[aliasedSource(resolve(filePath, mdLink))] ||
71
- sourceToPermalink[aliasedSource(`${contentPathLocalized}/${mdLink}`)] ||
72
- sourceToPermalink[aliasedSource(`${contentPath}/${mdLink}`)];
129
+ const permalink: string | undefined = aliasedSourceMatch
130
+ ? sourceToPermalink[aliasedSourceMatch]
131
+ : undefined;
73
132
 
74
133
  if (permalink) {
75
- modifiedLine = modifiedLine.replace(mdLink, permalink);
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 must be composed of letters,
18
+ * underscores, and dashes only.
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;
30
+ } {
31
+ const customHeadingIdRegex = /\s*\{#(?<id>[\w-]+)\}$/;
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
+ }