@docusaurus/mdx-loader 2.0.0-beta.fc64c12e4 → 2.0.0

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 (84) hide show
  1. package/lib/index.d.ts +32 -0
  2. package/lib/index.d.ts.map +1 -0
  3. package/lib/index.js +11 -0
  4. package/lib/index.js.map +1 -0
  5. package/lib/loader.d.ts +41 -0
  6. package/lib/loader.d.ts.map +1 -0
  7. package/lib/loader.js +210 -0
  8. package/lib/loader.js.map +1 -0
  9. package/lib/remark/admonitions/index.d.ts +15 -0
  10. package/lib/remark/admonitions/index.d.ts.map +1 -0
  11. package/lib/remark/admonitions/index.js +132 -0
  12. package/lib/remark/admonitions/index.js.map +1 -0
  13. package/lib/remark/headings/index.d.ts +9 -0
  14. package/lib/remark/headings/index.d.ts.map +1 -0
  15. package/lib/remark/headings/index.js +58 -0
  16. package/lib/remark/headings/index.js.map +1 -0
  17. package/lib/remark/toc/index.d.ts +9 -0
  18. package/lib/remark/toc/index.d.ts.map +1 -0
  19. package/lib/remark/toc/index.js +81 -0
  20. package/lib/remark/toc/index.js.map +1 -0
  21. package/lib/remark/transformImage/index.d.ts +13 -0
  22. package/lib/remark/transformImage/index.d.ts.map +1 -0
  23. package/lib/remark/transformImage/index.js +114 -0
  24. package/lib/remark/transformImage/index.js.map +1 -0
  25. package/lib/remark/transformLinks/index.d.ts +13 -0
  26. package/lib/remark/transformLinks/index.d.ts.map +1 -0
  27. package/lib/remark/transformLinks/index.js +106 -0
  28. package/lib/remark/transformLinks/index.js.map +1 -0
  29. package/lib/remark/unwrapMdxCodeBlocks/index.d.ts +9 -0
  30. package/lib/remark/unwrapMdxCodeBlocks/index.d.ts.map +1 -0
  31. package/lib/remark/unwrapMdxCodeBlocks/index.js +29 -0
  32. package/lib/remark/unwrapMdxCodeBlocks/index.js.map +1 -0
  33. package/lib/remark/utils/index.d.ts +11 -0
  34. package/lib/remark/utils/index.d.ts.map +1 -0
  35. package/lib/remark/utils/index.js +38 -0
  36. package/lib/remark/utils/index.js.map +1 -0
  37. package/package.json +26 -21
  38. package/src/deps.d.ts +31 -0
  39. package/src/index.ts +35 -0
  40. package/src/loader.ts +282 -0
  41. package/src/remark/admonitions/LICENSE +45 -0
  42. package/src/remark/admonitions/README.md +3 -0
  43. package/src/remark/admonitions/index.ts +186 -0
  44. package/src/remark/headings/{index.js → index.ts} +23 -27
  45. package/src/remark/toc/index.ts +98 -0
  46. package/src/remark/transformImage/index.ts +173 -0
  47. package/src/remark/transformLinks/index.ts +164 -0
  48. package/src/remark/unwrapMdxCodeBlocks/{index.js → index.ts} +14 -15
  49. package/src/remark/utils/index.ts +36 -0
  50. package/src/index.d.ts +0 -19
  51. package/src/index.js +0 -133
  52. package/src/remark/headings/__tests__/index.test.js +0 -315
  53. package/src/remark/toc/__tests__/__snapshots__/index.test.js.snap +0 -85
  54. package/src/remark/toc/__tests__/fixtures/empty-headings.md +0 -5
  55. package/src/remark/toc/__tests__/fixtures/inline-code.md +0 -9
  56. package/src/remark/toc/__tests__/fixtures/insert-below-imports.md +0 -11
  57. package/src/remark/toc/__tests__/fixtures/just-content.md +0 -15
  58. package/src/remark/toc/__tests__/fixtures/name-exist.md +0 -7
  59. package/src/remark/toc/__tests__/fixtures/non-text-content.md +0 -9
  60. package/src/remark/toc/__tests__/index.test.js +0 -212
  61. package/src/remark/toc/index.js +0 -79
  62. package/src/remark/toc/search.js +0 -73
  63. package/src/remark/transformImage/__tests__/__snapshots__/index.test.js.snap +0 -31
  64. package/src/remark/transformImage/__tests__/fixtures/fail.md +0 -1
  65. package/src/remark/transformImage/__tests__/fixtures/img.md +0 -17
  66. package/src/remark/transformImage/__tests__/fixtures/img.png +0 -0
  67. package/src/remark/transformImage/__tests__/fixtures/noUrl.md +0 -1
  68. package/src/remark/transformImage/__tests__/fixtures/pathname.md +0 -1
  69. package/src/remark/transformImage/__tests__/index.test.js +0 -59
  70. package/src/remark/transformImage/index.js +0 -106
  71. package/src/remark/transformLinks/__tests__/__snapshots__/index.test.js.snap +0 -41
  72. package/src/remark/transformLinks/__tests__/fixtures/asset.md +0 -29
  73. package/src/remark/transformLinks/__tests__/fixtures/asset.pdf +0 -0
  74. package/src/remark/transformLinks/__tests__/fixtures/noUrl.md +0 -1
  75. package/src/remark/transformLinks/__tests__/fixtures/pathname.md +0 -1
  76. package/src/remark/transformLinks/__tests__/fixtures/static/staticAsset.pdf +0 -0
  77. package/src/remark/transformLinks/__tests__/fixtures/static/staticAssetImage.png +0 -0
  78. package/src/remark/transformLinks/__tests__/index.test.js +0 -44
  79. package/src/remark/transformLinks/index.js +0 -143
  80. package/src/remark/unwrapMdxCodeBlocks/__tests__/__snapshots__/index.test.js.snap +0 -819
  81. package/src/remark/unwrapMdxCodeBlocks/__tests__/fixtures/has-mdx-code-blocks.mdx +0 -95
  82. package/src/remark/unwrapMdxCodeBlocks/__tests__/index.test.js +0 -38
  83. package/src/remark/utils/index.js +0 -39
  84. package/tsconfig.json +0 -9
package/lib/index.d.ts ADDED
@@ -0,0 +1,32 @@
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
+ import { mdxLoader } from './loader';
8
+ export default mdxLoader;
9
+ export declare type TOCItem = {
10
+ readonly value: string;
11
+ readonly id: string;
12
+ readonly level: number;
13
+ };
14
+ export declare type LoadedMDXContent<FrontMatter, Metadata, Assets = undefined> = {
15
+ /** As verbatim declared in the MDX document. */
16
+ readonly frontMatter: FrontMatter;
17
+ /** As provided by the content plugin. */
18
+ readonly metadata: Metadata;
19
+ /** A list of TOC items (headings). */
20
+ readonly toc: readonly TOCItem[];
21
+ /** First h1 title before any content. */
22
+ readonly contentTitle: string | undefined;
23
+ /**
24
+ * Usually image assets that may be collocated like `./img/thumbnail.png`.
25
+ * The loader would also bundle these assets and the client should use these
26
+ * in priority.
27
+ */
28
+ readonly assets: Assets;
29
+ (): JSX.Element;
30
+ };
31
+ export type { Options, MDXPlugin, MDXOptions } from './loader';
32
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAC,SAAS,EAAC,MAAM,UAAU,CAAC;AAEnC,eAAe,SAAS,CAAC;AAEzB,oBAAY,OAAO,GAAG;IACpB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;CACxB,CAAC;AAEF,oBAAY,gBAAgB,CAAC,WAAW,EAAE,QAAQ,EAAE,MAAM,GAAG,SAAS,IAAI;IACxE,gDAAgD;IAChD,QAAQ,CAAC,WAAW,EAAE,WAAW,CAAC;IAClC,yCAAyC;IACzC,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC;IAC5B,sCAAsC;IACtC,QAAQ,CAAC,GAAG,EAAE,SAAS,OAAO,EAAE,CAAC;IACjC,yCAAyC;IACzC,QAAQ,CAAC,YAAY,EAAE,MAAM,GAAG,SAAS,CAAC;IAC1C;;;;OAIG;IACH,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,IAAI,GAAG,CAAC,OAAO,CAAC;CACjB,CAAC;AACF,YAAY,EAAC,OAAO,EAAE,SAAS,EAAE,UAAU,EAAC,MAAM,UAAU,CAAC"}
package/lib/index.js ADDED
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) Facebook, Inc. and its affiliates.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ const loader_1 = require("./loader");
10
+ exports.default = loader_1.mdxLoader;
11
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;AAEH,qCAAmC;AAEnC,kBAAe,kBAAS,CAAC"}
@@ -0,0 +1,41 @@
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
+ import type { LoaderContext } from 'webpack';
8
+ import type { Plugin } from 'unified';
9
+ import type { AdmonitionOptions } from './remark/admonitions';
10
+ export declare type MDXPlugin = [
11
+ Plugin<any[]>,
12
+ any
13
+ ] | Plugin<any[]>;
14
+ export declare type MDXOptions = {
15
+ admonitions: boolean | AdmonitionOptions;
16
+ remarkPlugins: MDXPlugin[];
17
+ rehypePlugins: MDXPlugin[];
18
+ beforeDefaultRemarkPlugins: MDXPlugin[];
19
+ beforeDefaultRehypePlugins: MDXPlugin[];
20
+ };
21
+ export declare type Options = Partial<MDXOptions> & {
22
+ staticDirs: string[];
23
+ siteDir: string;
24
+ isMDXPartial?: (filePath: string) => boolean;
25
+ isMDXPartialFrontMatterWarningDisabled?: boolean;
26
+ removeContentTitle?: boolean;
27
+ metadataPath?: string | ((filePath: string) => string);
28
+ createAssets?: (metadata: {
29
+ frontMatter: {
30
+ [key: string]: unknown;
31
+ };
32
+ metadata: {
33
+ [key: string]: unknown;
34
+ };
35
+ }) => {
36
+ [key: string]: unknown;
37
+ };
38
+ filepath: string;
39
+ };
40
+ export declare function mdxLoader(this: LoaderContext<Options>, fileString: string): Promise<void>;
41
+ //# sourceMappingURL=loader.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"loader.d.ts","sourceRoot":"","sources":["../src/loader.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAqBH,OAAO,KAAK,EAAC,aAAa,EAAC,MAAM,SAAS,CAAC;AAC3C,OAAO,KAAK,EAAY,MAAM,EAAC,MAAM,SAAS,CAAC;AAC/C,OAAO,KAAK,EAA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 +1,210 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) Facebook, Inc. and its affiliates.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.mdxLoader = void 0;
10
+ const tslib_1 = require("tslib");
11
+ const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
12
+ const logger_1 = tslib_1.__importDefault(require("@docusaurus/logger"));
13
+ const utils_1 = require("@docusaurus/utils");
14
+ const mdx_1 = require("@mdx-js/mdx");
15
+ const remark_emoji_1 = tslib_1.__importDefault(require("remark-emoji"));
16
+ const stringify_object_1 = tslib_1.__importDefault(require("stringify-object"));
17
+ const headings_1 = tslib_1.__importDefault(require("./remark/headings"));
18
+ const toc_1 = tslib_1.__importDefault(require("./remark/toc"));
19
+ const unwrapMdxCodeBlocks_1 = tslib_1.__importDefault(require("./remark/unwrapMdxCodeBlocks"));
20
+ const transformImage_1 = tslib_1.__importDefault(require("./remark/transformImage"));
21
+ const transformLinks_1 = tslib_1.__importDefault(require("./remark/transformLinks"));
22
+ const admonitions_1 = tslib_1.__importDefault(require("./remark/admonitions"));
23
+ const { loaders: { inlineMarkdownImageFileLoader }, } = (0, utils_1.getFileLoaderUtils)();
24
+ const pragma = `
25
+ /* @jsxRuntime classic */
26
+ /* @jsx mdx */
27
+ /* @jsxFrag React.Fragment */
28
+ `;
29
+ const DEFAULT_OPTIONS = {
30
+ admonitions: true,
31
+ rehypePlugins: [],
32
+ remarkPlugins: [unwrapMdxCodeBlocks_1.default, remark_emoji_1.default, headings_1.default, toc_1.default],
33
+ beforeDefaultRemarkPlugins: [],
34
+ beforeDefaultRehypePlugins: [],
35
+ };
36
+ const compilerCache = new Map();
37
+ /**
38
+ * When this throws, it generally means that there's no metadata file associated
39
+ * with this MDX document. It can happen when using MDX partials (usually
40
+ * starting with _). That's why it's important to provide the `isMDXPartial`
41
+ * function in config
42
+ */
43
+ async function readMetadataPath(metadataPath) {
44
+ try {
45
+ return await fs_extra_1.default.readFile(metadataPath, 'utf8');
46
+ }
47
+ catch (err) {
48
+ logger_1.default.error `MDX loader can't read MDX metadata file path=${metadataPath}. Maybe the isMDXPartial option function was not provided?`;
49
+ throw err;
50
+ }
51
+ }
52
+ /**
53
+ * Converts assets an object with Webpack require calls code.
54
+ * This is useful for mdx files to reference co-located assets using relative
55
+ * paths. Those assets should enter the Webpack assets pipeline and be hashed.
56
+ * For now, we only handle that for images and paths starting with `./`:
57
+ *
58
+ * `{image: "./myImage.png"}` => `{image: require("./myImage.png")}`
59
+ */
60
+ function createAssetsExportCode(assets) {
61
+ if (typeof assets !== 'object' ||
62
+ !assets ||
63
+ Object.keys(assets).length === 0) {
64
+ return 'undefined';
65
+ }
66
+ // TODO implementation can be completed/enhanced
67
+ function createAssetValueCode(assetValue) {
68
+ if (Array.isArray(assetValue)) {
69
+ const arrayItemCodes = assetValue.map((item) => createAssetValueCode(item) ?? 'undefined');
70
+ return `[${arrayItemCodes.join(', ')}]`;
71
+ }
72
+ // Only process string values starting with ./
73
+ // We could enhance this logic and check if file exists on disc?
74
+ if (typeof assetValue === 'string' && assetValue.startsWith('./')) {
75
+ // TODO do we have other use-cases than image assets?
76
+ // Probably not worth adding more support, as we want to move to Webpack 5 new asset system (https://github.com/facebook/docusaurus/pull/4708)
77
+ const inlineLoader = inlineMarkdownImageFileLoader;
78
+ return `require("${inlineLoader}${(0, utils_1.escapePath)(assetValue)}").default`;
79
+ }
80
+ return undefined;
81
+ }
82
+ const assetEntries = Object.entries(assets);
83
+ const codeLines = assetEntries
84
+ .map(([key, value]) => {
85
+ const assetRequireCode = createAssetValueCode(value);
86
+ return assetRequireCode ? `"${key}": ${assetRequireCode},` : undefined;
87
+ })
88
+ .filter(Boolean);
89
+ return `{\n${codeLines.join('\n')}\n}`;
90
+ }
91
+ function getAdmonitionsPlugins(admonitionsOption) {
92
+ if (admonitionsOption) {
93
+ const plugin = admonitionsOption === true
94
+ ? admonitions_1.default
95
+ : [admonitions_1.default, admonitionsOption];
96
+ return [plugin];
97
+ }
98
+ return [];
99
+ }
100
+ async function mdxLoader(fileString) {
101
+ const callback = this.async();
102
+ const filePath = this.resourcePath;
103
+ const reqOptions = this.getOptions();
104
+ const { frontMatter, content: contentWithTitle } = (0, utils_1.parseFrontMatter)(fileString);
105
+ const { content, contentTitle } = (0, utils_1.parseMarkdownContentTitle)(contentWithTitle, {
106
+ removeContentTitle: reqOptions.removeContentTitle,
107
+ });
108
+ const hasFrontMatter = Object.keys(frontMatter).length > 0;
109
+ if (!compilerCache.has(this.query)) {
110
+ const remarkPlugins = [
111
+ ...(reqOptions.beforeDefaultRemarkPlugins ?? []),
112
+ ...getAdmonitionsPlugins(reqOptions.admonitions ?? false),
113
+ ...DEFAULT_OPTIONS.remarkPlugins,
114
+ [
115
+ transformImage_1.default,
116
+ {
117
+ staticDirs: reqOptions.staticDirs,
118
+ siteDir: reqOptions.siteDir,
119
+ },
120
+ ],
121
+ [
122
+ transformLinks_1.default,
123
+ {
124
+ staticDirs: reqOptions.staticDirs,
125
+ siteDir: reqOptions.siteDir,
126
+ },
127
+ ],
128
+ ...(reqOptions.remarkPlugins ?? []),
129
+ ];
130
+ const rehypePlugins = [
131
+ ...(reqOptions.beforeDefaultRehypePlugins ?? []),
132
+ ...DEFAULT_OPTIONS.rehypePlugins,
133
+ ...(reqOptions.rehypePlugins ?? []),
134
+ ];
135
+ const options = {
136
+ ...reqOptions,
137
+ remarkPlugins,
138
+ rehypePlugins,
139
+ };
140
+ compilerCache.set(this.query, [(0, mdx_1.createCompiler)(options), options]);
141
+ }
142
+ const [compiler, options] = compilerCache.get(this.query);
143
+ let result;
144
+ try {
145
+ result = await compiler
146
+ .process({
147
+ contents: content,
148
+ path: this.resourcePath,
149
+ })
150
+ .then((res) => res.toString());
151
+ }
152
+ catch (err) {
153
+ return callback(err);
154
+ }
155
+ // MDX partials are MDX files starting with _ or in a folder starting with _
156
+ // Partial are not expected to have associated metadata files or front matter
157
+ const isMDXPartial = options.isMDXPartial?.(filePath);
158
+ if (isMDXPartial && hasFrontMatter) {
159
+ const errorMessage = `Docusaurus MDX partial files should not contain front matter.
160
+ Those partial files use the _ prefix as a convention by default, but this is configurable.
161
+ File at ${filePath} contains front matter that will be ignored:
162
+ ${JSON.stringify(frontMatter, null, 2)}`;
163
+ if (!options.isMDXPartialFrontMatterWarningDisabled) {
164
+ const shouldError = process.env.NODE_ENV === 'test' || process.env.CI;
165
+ if (shouldError) {
166
+ return callback(new Error(errorMessage));
167
+ }
168
+ logger_1.default.warn(errorMessage);
169
+ }
170
+ }
171
+ function getMetadataPath() {
172
+ if (!isMDXPartial) {
173
+ // Read metadata for this MDX and export it.
174
+ if (options.metadataPath && typeof options.metadataPath === 'function') {
175
+ return options.metadataPath(filePath);
176
+ }
177
+ }
178
+ return undefined;
179
+ }
180
+ const metadataPath = getMetadataPath();
181
+ if (metadataPath) {
182
+ this.addDependency(metadataPath);
183
+ }
184
+ const metadataJsonString = metadataPath
185
+ ? await readMetadataPath(metadataPath)
186
+ : undefined;
187
+ const metadata = metadataJsonString
188
+ ? JSON.parse(metadataJsonString)
189
+ : undefined;
190
+ const assets = reqOptions.createAssets && metadata
191
+ ? reqOptions.createAssets({ frontMatter, metadata })
192
+ : undefined;
193
+ const exportsCode = `
194
+ export const frontMatter = ${(0, stringify_object_1.default)(frontMatter)};
195
+ export const contentTitle = ${(0, stringify_object_1.default)(contentTitle)};
196
+ ${metadataJsonString ? `export const metadata = ${metadataJsonString};` : ''}
197
+ ${assets ? `export const assets = ${createAssetsExportCode(assets)};` : ''}
198
+ `;
199
+ const code = `
200
+ ${pragma}
201
+ import React from 'react';
202
+ import { mdx } from '@mdx-js/react';
203
+
204
+ ${exportsCode}
205
+ ${result}
206
+ `;
207
+ return callback(null, code);
208
+ }
209
+ exports.mdxLoader = mdxLoader;
210
+ //# sourceMappingURL=loader.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"loader.js","sour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 *
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
+ import type { Plugin } from 'unified';
8
+ export declare type AdmonitionOptions = {
9
+ tag: string;
10
+ keywords: string[];
11
+ };
12
+ export declare const DefaultAdmonitionOptions: AdmonitionOptions;
13
+ declare const plugin: Plugin;
14
+ export default plugin;
15
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/remark/admonitions/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,KAAK,EAAyB,MAAM,EAAC,MAAM,SAAS,CAAC;AAK5D,oBAAY,iBAAiB,GAAG;IAC9B,GAAG,EAAE,MAAM,CAAC;IACZ,QAAQ,EAAE,MAAM,EAAE,CAAC;CACpB,CAAC;AAEF,eAAO,MAAM,wBAAwB,EAAE,iBAatC,CAAC;AAgBF,QAAA,MAAM,MAAM,EAAE,MAwIb,CAAC;AAEF,eAAe,MAAM,CAAC"}
@@ -0,0 +1,132 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) Facebook, Inc. and its affiliates.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.DefaultAdmonitionOptions = void 0;
10
+ const tslib_1 = require("tslib");
11
+ const unist_util_visit_1 = tslib_1.__importDefault(require("unist-util-visit"));
12
+ const NEWLINE = '\n';
13
+ exports.DefaultAdmonitionOptions = {
14
+ tag: ':::',
15
+ keywords: [
16
+ 'secondary',
17
+ 'info',
18
+ 'success',
19
+ 'danger',
20
+ 'note',
21
+ 'tip',
22
+ 'warning',
23
+ 'important',
24
+ 'caution',
25
+ ],
26
+ };
27
+ function escapeRegExp(s) {
28
+ return s.replace(/[-[\]{}()*+?.\\^$|/]/g, '\\$&');
29
+ }
30
+ function normalizeOptions(options) {
31
+ return { ...exports.DefaultAdmonitionOptions, ...options };
32
+ }
33
+ // This string value does not matter much
34
+ // It is ignored because nodes are using hName/hProperties coming from HAST
35
+ const admonitionNodeType = 'admonitionHTML';
36
+ const plugin = function plugin(optionsInput = {}) {
37
+ const options = normalizeOptions(optionsInput);
38
+ const keywords = Object.values(options.keywords).map(escapeRegExp).join('|');
39
+ const tag = escapeRegExp(options.tag);
40
+ const regex = new RegExp(`${tag}(${keywords})(?: *(.*))?\n`);
41
+ const escapeTag = new RegExp(escapeRegExp(`\\${options.tag}`), 'g');
42
+ // The tokenizer is called on blocks to determine if there is an admonition
43
+ // present and create tags for it
44
+ function blockTokenizer(eat, value, silent) {
45
+ // Stop if no match or match does not start at beginning of line
46
+ const match = regex.exec(value);
47
+ if (!match || match.index !== 0) {
48
+ return false;
49
+ }
50
+ // If silent return the match
51
+ if (silent) {
52
+ return true;
53
+ }
54
+ const now = eat.now();
55
+ const [opening, keyword, title] = match;
56
+ const food = [];
57
+ const content = [];
58
+ let newValue = value;
59
+ // consume lines until a closing tag
60
+ let idx = newValue.indexOf(NEWLINE);
61
+ while (idx !== -1) {
62
+ // grab this line and eat it
63
+ const next = newValue.indexOf(NEWLINE, idx + 1);
64
+ const line = next !== -1 ? newValue.slice(idx + 1, next) : newValue.slice(idx + 1);
65
+ food.push(line);
66
+ newValue = newValue.slice(idx + 1);
67
+ // the closing tag is NOT part of the content
68
+ if (line.startsWith(options.tag)) {
69
+ break;
70
+ }
71
+ content.push(line);
72
+ idx = newValue.indexOf(NEWLINE);
73
+ }
74
+ // consume the processed tag and replace escape sequences
75
+ const contentString = content.join(NEWLINE).replace(escapeTag, options.tag);
76
+ const add = eat(opening + food.join(NEWLINE));
77
+ // parse the content in block mode
78
+ const exit = this.enterBlock();
79
+ const contentNodes = this.tokenizeBlock(contentString, now);
80
+ exit();
81
+ const titleNodes = this.tokenizeInline(title, now);
82
+ const isSimpleTextTitle = titleNodes.length === 1 && titleNodes[0].type === 'text';
83
+ const element = {
84
+ type: admonitionNodeType,
85
+ data: {
86
+ // hName/hProperties come from HAST
87
+ // See https://github.com/syntax-tree/mdast-util-to-hast#fields-on-nodes
88
+ hName: 'admonition',
89
+ hProperties: {
90
+ ...(title && isSimpleTextTitle && { title }),
91
+ type: keyword,
92
+ },
93
+ },
94
+ children: [
95
+ // For titles containing MDX syntax: create a custom element. The theme
96
+ // component will extract it and render it nicely.
97
+ //
98
+ // Temporary workaround, because it's complex in MDX v1 to emit
99
+ // interpolated JSX prop syntax (title={<>my <code>title</code></>}).
100
+ // For this reason, we use children instead of the title prop.
101
+ title &&
102
+ !isSimpleTextTitle && {
103
+ type: admonitionNodeType,
104
+ data: {
105
+ hName: 'mdxAdmonitionTitle',
106
+ hProperties: {},
107
+ },
108
+ children: titleNodes,
109
+ },
110
+ ...contentNodes,
111
+ ].filter(Boolean),
112
+ };
113
+ return add(element);
114
+ }
115
+ // add tokenizer to parser after fenced code blocks
116
+ const Parser = this.Parser.prototype;
117
+ Parser.blockTokenizers.admonition = blockTokenizer;
118
+ Parser.blockMethods.splice(Parser.blockMethods.indexOf('fencedCode') + 1, 0, 'admonition');
119
+ Parser.interruptParagraph.splice(Parser.interruptParagraph.indexOf('fencedCode') + 1, 0, ['admonition']);
120
+ Parser.interruptList.splice(Parser.interruptList.indexOf('fencedCode') + 1, 0, ['admonition']);
121
+ Parser.interruptBlockquote.splice(Parser.interruptBlockquote.indexOf('fencedCode') + 1, 0, ['admonition']);
122
+ return (root) => {
123
+ // escape everything except admonitionHTML nodes
124
+ (0, unist_util_visit_1.default)(root, (node) => node?.type !== admonitionNodeType, (node) => {
125
+ if (node.value) {
126
+ node.value = node.value.replace(escapeTag, options.tag);
127
+ }
128
+ });
129
+ };
130
+ };
131
+ exports.default = plugin;
132
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/remark/admonitions/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;AAEH,gFAAqC;AAIrC,MAAM,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ACrC,MAAM,CAAC,eAAe,CAAC,UAAU,GAAG,cAAc,CAAC;IACnD,MAAM,CAAC,YAAY,CAAC,MAAM,CACxB,MAAM,CAAC,YAAY,CAAC,OAAO,CAAC,YAAY,CAAC,GAAG,CAAC,EAC7C,CAAC,EACD,YAAY,CACb,CAAC;IACF,MAAM,CAAC,kBAAkB,CAAC,MAAM,CAC9B,MAAM,CAAC,kBAAkB,CAAC,OAAO,CAAC,YAAY,CAAC,GAAG,CAAC,EACnD,CAAC,EACD,CAAC,YAAY,CAAC,CACf,CAAC;IACF,MAAM,CAAC,aAAa,CAAC,MAAM,CACzB,MAAM,CAAC,aAAa,CAAC,OAAO,CAAC,YAAY,CAAC,GAAG,CAAC,EAC9C,CAAC,EACD,CAAC,YAAY,CAAC,CACf,CAAC;IACF,MAAM,CAAC,mBAAmB,CAAC,MAAM,CAC/B,MAAM,CAAC,mBAAmB,CAAC,OAAO,CAAC,YAAY,CAAC,GAAG,CAAC,EACpD,CAAC,EACD,CAAC,YAAY,CAAC,CACf,CAAC;IAEF,OAAO,CAAC,IAAI,EAAE,EAAE;QACd,gDAAgD;QAChD,IAAA,0BAAK,EACH,IAAI,EACJ,CAAC,IAAa,EAAmB,EAAE,CAChC,IAA4B,EAAE,IAAI,KAAK,kBAAkB,EAC5D,CAAC,IAAa,EAAE,EAAE;YAChB,IAAI,IAAI,CAAC,KAAK,EAAE;gBACd,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,SAAS,EAAE,OAAO,CAAC,GAAG,CAAC,CAAC;aACzD;QACH,CAAC,CACF,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC,CAAC;AAEF,kBAAe,MAAM,CAAC"}
@@ -0,0 +1,9 @@
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
+ import type { Transformer } from 'unified';
8
+ export default function plugin(): Transformer;
9
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/remark/headings/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAOH,OAAO,KAAK,EAAC,WAAW,EAAC,MAAM,SAAS,CAAC;AAGzC,MAAM,CAAC,OAAO,UAAU,MAAM,IAAI,WAAW,CAsD5C"}
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) Facebook, Inc. and its affiliates.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ const tslib_1 = require("tslib");
10
+ /* Based on remark-slug (https://github.com/remarkjs/remark-slug) and gatsby-remark-autolink-headers (https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-remark-autolink-headers) */
11
+ const utils_1 = require("@docusaurus/utils");
12
+ const unist_util_visit_1 = tslib_1.__importDefault(require("unist-util-visit"));
13
+ const mdast_util_to_string_1 = tslib_1.__importDefault(require("mdast-util-to-string"));
14
+ function plugin() {
15
+ return (root) => {
16
+ const slugs = (0, utils_1.createSlugger)();
17
+ (0, unist_util_visit_1.default)(root, 'heading', (headingNode) => {
18
+ const data = headingNode.data ?? (headingNode.data = {});
19
+ const properties = (data.hProperties || (data.hProperties = {}));
20
+ let { id } = properties;
21
+ if (id) {
22
+ id = slugs.slug(id, { maintainCase: true });
23
+ }
24
+ else {
25
+ const headingTextNodes = headingNode.children.filter(({ type }) => !['html', 'jsx'].includes(type));
26
+ const heading = (0, mdast_util_to_string_1.default)(headingTextNodes.length > 0 ? headingTextNodes : headingNode);
27
+ // Support explicit heading IDs
28
+ const parsedHeading = (0, utils_1.parseMarkdownHeadingId)(heading);
29
+ id = parsedHeading.id ?? slugs.slug(heading);
30
+ if (parsedHeading.id) {
31
+ // When there's an id, it is always in the last child node
32
+ // Sometimes heading is in multiple "parts" (** syntax creates a child
33
+ // node):
34
+ // ## part1 *part2* part3 {#id}
35
+ const lastNode = headingNode.children[headingNode.children.length - 1];
36
+ if (headingNode.children.length > 1) {
37
+ const lastNodeText = (0, utils_1.parseMarkdownHeadingId)(lastNode.value).text;
38
+ // When last part contains test+id, remove the id
39
+ if (lastNodeText) {
40
+ lastNode.value = lastNodeText;
41
+ }
42
+ // When last part contains only the id: completely remove that node
43
+ else {
44
+ headingNode.children.pop();
45
+ }
46
+ }
47
+ else {
48
+ lastNode.value = parsedHeading.text;
49
+ }
50
+ }
51
+ }
52
+ data.id = id;
53
+ properties.id = id;
54
+ });
55
+ };
56
+ }
57
+ exports.default = plugin;
58
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/remark/headings/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,gMAAgM;AAEhM,6CAAwE;AACxE,gFAAqC;AACrC,wFAAiD;AAIjD,SAAwB,MAAM;IAC5B,OAAO,CAAC,IAAI,EAAE,EAAE;QACd,MAAM,KAAK,GAAG,IAAA,qBAAa,GAAE,CAAC;QAC9B,IAAA,0BAAK,EAAC,IAAI,EAAE,SAAS,EAAE,CAAC,WAAoB,EAAE,EAAE;YAC9C,MAAM,IAAI,GAAG,WAAW,CAAC,IAAI,IAAI,CAAC,WAAW,CAAC,IAAI,GAAG,EAAE,CAAC,CAAC;YACzD,MAAM,UAAU,GAAG,CAAC,IAAI,CAAC,WAAW,IAAI,CAAC,IAAI,CAAC,WAAW,GAAG,EAAE,CAAC,CAE9D,CAAC;YACF,IAAI,EAAC,EAAE,EAAC,GAAG,UAAU,CAAC;YAEtB,IAAI,EAAE,EAAE;gBACN,EAAE,GAAG,KAAK,CAAC,IAAI,CAAC,EAAE,EAAE,EAAC,YAAY,EAAE,IAAI,EAAC,CAAC,CAAC;aAC3C;iBAAM;gBACL,MAAM,gBAAgB,GAAG,WAAW,CAAC,QAAQ,CAAC,MA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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
+ import type { Transformer } from 'unified';
8
+ export default function plugin(): Transformer;
9
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/remark/toc/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAaH,OAAO,KAAK,EAAC,WAAW,EAAC,MAAM,SAAS,CAAC;AAoDzC,MAAM,CAAC,OAAO,UAAU,MAAM,IAAI,WAAW,CA2B5C"}
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) Facebook, Inc. and its affiliates.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ const tslib_1 = require("tslib");
10
+ const parser_1 = require("@babel/parser");
11
+ const traverse_1 = tslib_1.__importDefault(require("@babel/traverse"));
12
+ const stringify_object_1 = tslib_1.__importDefault(require("stringify-object"));
13
+ const mdast_util_to_string_1 = tslib_1.__importDefault(require("mdast-util-to-string"));
14
+ const unist_util_visit_1 = tslib_1.__importDefault(require("unist-util-visit"));
15
+ const utils_1 = require("../utils");
16
+ const parseOptions = {
17
+ plugins: ['jsx'],
18
+ sourceType: 'module',
19
+ };
20
+ const name = 'toc';
21
+ const isImport = (child) => child.type === 'import';
22
+ const hasImports = (index) => index > -1;
23
+ const isExport = (child) => child.type === 'export';
24
+ const isTarget = (child) => {
25
+ let found = false;
26
+ const ast = (0, parser_1.parse)(child.value, parseOptions);
27
+ (0, traverse_1.default)(ast, {
28
+ VariableDeclarator: (path) => {
29
+ if (path.node.id.name === name) {
30
+ found = true;
31
+ }
32
+ },
33
+ });
34
+ return found;
35
+ };
36
+ const getOrCreateExistingTargetIndex = (children) => {
37
+ let importsIndex = -1;
38
+ let targetIndex = -1;
39
+ children.forEach((child, index) => {
40
+ if (isImport(child)) {
41
+ importsIndex = index;
42
+ }
43
+ else if (isExport(child) && isTarget(child)) {
44
+ targetIndex = index;
45
+ }
46
+ });
47
+ if (targetIndex === -1) {
48
+ const target = {
49
+ default: false,
50
+ type: 'export',
51
+ value: `export const ${name} = [];`,
52
+ };
53
+ targetIndex = hasImports(importsIndex) ? importsIndex + 1 : 0;
54
+ children.splice(targetIndex, 0, target);
55
+ }
56
+ return targetIndex;
57
+ };
58
+ function plugin() {
59
+ return (root) => {
60
+ const headings = [];
61
+ (0, unist_util_visit_1.default)(root, 'heading', (child, index, parent) => {
62
+ const value = (0, mdast_util_to_string_1.default)(child);
63
+ // depth: 1 headings are titles and not included in the TOC
64
+ if (parent !== root || !value || child.depth < 2) {
65
+ return;
66
+ }
67
+ headings.push({
68
+ value: (0, utils_1.toValue)(child),
69
+ id: child.data.id,
70
+ level: child.depth,
71
+ });
72
+ });
73
+ const { children } = root;
74
+ const targetIndex = getOrCreateExistingTargetIndex(children);
75
+ if (headings.length) {
76
+ children[targetIndex].value = `export const ${name} = ${(0, stringify_object_1.default)(headings)};`;
77
+ }
78
+ };
79
+ }
80
+ exports.default = plugin;
81
+ //# sourceMappingURL=index.js.map