@docusaurus/core 4.0.0-canary-6972 → 4.0.0-canary-6973

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.
@@ -141,7 +141,7 @@ async function createCLIProgram({ cli, cliArgs, siteDir, config, }) {
141
141
  cli
142
142
  .command('write-heading-ids [siteDir] [files...]')
143
143
  .description('Generate heading ids in Markdown content.')
144
- .option('--syntax <syntax>', 'heading ID syntax: "classic" ({#id}) or "mdx-comment" ({/* #id */}) (default: "classic")')
144
+ .addOption(new commander_1.Option('--syntax <syntax>', 'heading ID syntax: "classic" ({#id}) or "mdx-comment" ({/* #id */}) (default: "classic" for files compiled as CommonMark, "mdx-comment" for files compiled as MDX)').choices(writeHeadingIds_1.HeadingIdSyntaxes))
145
145
  .option('--migrate', 'migrate existing heading IDs to the target --syntax, if they are using a different syntax (default: false)')
146
146
  .option('--overwrite', 'overwrite existing heading IDs, re-generate them from the heading text (default: false)')
147
147
  .option('--maintain-case', "keep the headings' casing, otherwise make all lowercase (default: false)")
@@ -5,4 +5,5 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
  import { type WriteHeadingIDOptions } from '@docusaurus/utils';
8
+ export declare const HeadingIdSyntaxes: ["classic", "mdx-comment"];
8
9
  export declare function writeHeadingIds(siteDirParam?: string, files?: string[], options?: WriteHeadingIDOptions): Promise<void>;
@@ -6,29 +6,37 @@
6
6
  * LICENSE file in the root directory of this source tree.
7
7
  */
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.HeadingIdSyntaxes = void 0;
9
10
  exports.writeHeadingIds = writeHeadingIds;
10
11
  const tslib_1 = require("tslib");
12
+ const node_path_1 = tslib_1.__importDefault(require("node:path"));
11
13
  const logger_1 = tslib_1.__importDefault(require("@docusaurus/logger"));
12
14
  const utils_1 = require("@docusaurus/utils");
13
15
  const glob_1 = require("@docusaurus/glob");
14
16
  const fs_1 = require("@docusaurus/fs");
15
17
  const site_1 = require("../server/site");
16
18
  const init_1 = require("../server/plugins/init");
17
- function inferFallbackSyntax(_filepath) {
18
- // TODO Docusaurus v4 - infer the syntax based on the file extensions?
19
- // This is not ideal because we have many ways to define the syntax
20
- // (front matter "format", siteConfig.markdown.format etc...)
21
- // but probably good enough for now
22
- // Until then, we default to the classic syntax
23
- // The mdx-comment syntax is opt-in
24
- return 'classic';
25
- }
26
- function getHeadingIdSyntax(filepath, options) {
27
- return options?.syntax ?? inferFallbackSyntax(filepath);
19
+ exports.HeadingIdSyntaxes = [
20
+ 'classic',
21
+ 'mdx-comment',
22
+ ];
23
+ /**
24
+ * Infer the heading ID syntax that compiles for a given file:
25
+ * - `classic` ({#id}) for files compiled as CommonMark
26
+ * - `mdx-comment` ({/* #id * /}) for files compiled as MDX
27
+ *
28
+ * The front matter `format` is not taken into account, but users can still
29
+ * use the `--syntax` option to target a specific syntax.
30
+ */
31
+ function inferFallbackSyntax(filepath, markdownConfig) {
32
+ const isCommonMark = markdownConfig.format === 'detect'
33
+ ? node_path_1.default.extname(filepath) === '.md'
34
+ : markdownConfig.format === 'md';
35
+ return isCommonMark ? 'classic' : 'mdx-comment';
28
36
  }
29
- async function transformMarkdownFile(filepath, options) {
37
+ async function transformMarkdownFile(filepath, markdownConfig, options) {
30
38
  const content = await fs_1.fs.readFile(filepath, 'utf8');
31
- const syntax = getHeadingIdSyntax(filepath, options);
39
+ const syntax = options.syntax ?? inferFallbackSyntax(filepath, markdownConfig);
32
40
  const updatedContent = (0, utils_1.writeMarkdownHeadingId)(content, {
33
41
  ...options,
34
42
  syntax,
@@ -49,16 +57,14 @@ async function globMarkdownFiles(patterns) {
49
57
  * not belong to a content plugin. For example ./README.md should not be
50
58
  * transformed
51
59
  */
52
- async function getPathsToWatch(siteDir) {
53
- const context = await (0, site_1.loadContext)({ siteDir });
60
+ async function getPathsToWatch(context) {
54
61
  const plugins = await (0, init_1.initPlugins)(context);
55
62
  return plugins.flatMap((plugin) => plugin.getPathsToWatch?.() ?? []);
56
63
  }
57
- // TODO Docusaurus v4 - Upgrade commander, use choices() API?
64
+ // The CLI already validates --syntax, but not the programmatic API
58
65
  function validateOptions(options) {
59
- const validSyntaxValues = ['classic', 'mdx-comment'];
60
- if (options.syntax && !validSyntaxValues.includes(options.syntax)) {
61
- throw new Error(`Invalid --syntax value "${options.syntax}". Valid values: ${validSyntaxValues.join(', ')}`);
66
+ if (options.syntax && !exports.HeadingIdSyntaxes.includes(options.syntax)) {
67
+ throw new Error(`Invalid --syntax value "${options.syntax}". Valid values: ${exports.HeadingIdSyntaxes.join(', ')}`);
62
68
  }
63
69
  if (options.overwrite && options.migrate) {
64
70
  throw new Error("Options --overwrite and --migrate cannot be used together.\nThe --overwrite already re-generates IDs in the target syntax, so the --migrate option wouldn't have any effect.");
@@ -67,13 +73,14 @@ function validateOptions(options) {
67
73
  async function writeHeadingIds(siteDirParam = '.', files = [], options = {}) {
68
74
  validateOptions(options);
69
75
  const siteDir = await (0, fs_1.realpath)(siteDirParam);
70
- const patterns = files.length ? files : await getPathsToWatch(siteDir);
76
+ const context = await (0, site_1.loadContext)({ siteDir });
77
+ const patterns = files.length ? files : await getPathsToWatch(context);
71
78
  const markdownFiles = await globMarkdownFiles(patterns);
72
79
  if (markdownFiles.length === 0) {
73
80
  logger_1.default.warn `No markdown files found in siteDir path=${siteDir} for patterns: ${patterns}`;
74
81
  return;
75
82
  }
76
- const result = await Promise.all(markdownFiles.map((p) => transformMarkdownFile(p, options)));
83
+ const result = await Promise.all(markdownFiles.map((p) => transformMarkdownFile(p, context.siteConfig.markdown, options)));
77
84
  const pathsModified = result.filter(Boolean);
78
85
  if (pathsModified.length) {
79
86
  logger_1.default.success `Heading ids added to Markdown files (number=${`${pathsModified.length}/${markdownFiles.length}`} files): ${pathsModified}`;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@docusaurus/core",
3
3
  "description": "Easy to Maintain Open Source Documentation Websites",
4
- "version": "4.0.0-canary-6972",
4
+ "version": "4.0.0-canary-6973",
5
5
  "license": "MIT",
6
6
  "publishConfig": {
7
7
  "access": "public"
@@ -27,15 +27,15 @@
27
27
  "url": "https://github.com/facebook/docusaurus/issues"
28
28
  },
29
29
  "dependencies": {
30
- "@docusaurus/babel": "4.0.0-canary-6972",
31
- "@docusaurus/bundler": "4.0.0-canary-6972",
32
- "@docusaurus/fs": "4.0.0-canary-6972",
33
- "@docusaurus/glob": "4.0.0-canary-6972",
34
- "@docusaurus/logger": "4.0.0-canary-6972",
35
- "@docusaurus/mdx-loader": "4.0.0-canary-6972",
36
- "@docusaurus/utils": "4.0.0-canary-6972",
37
- "@docusaurus/utils-common": "4.0.0-canary-6972",
38
- "@docusaurus/utils-validation": "4.0.0-canary-6972",
30
+ "@docusaurus/babel": "4.0.0-canary-6973",
31
+ "@docusaurus/bundler": "4.0.0-canary-6973",
32
+ "@docusaurus/fs": "4.0.0-canary-6973",
33
+ "@docusaurus/glob": "4.0.0-canary-6973",
34
+ "@docusaurus/logger": "4.0.0-canary-6973",
35
+ "@docusaurus/mdx-loader": "4.0.0-canary-6973",
36
+ "@docusaurus/utils": "4.0.0-canary-6973",
37
+ "@docusaurus/utils-common": "4.0.0-canary-6973",
38
+ "@docusaurus/utils-validation": "4.0.0-canary-6973",
39
39
  "@rspack/core": "^2.2.3",
40
40
  "@rspack/dev-server": "^2.2.1",
41
41
  "boxen": "^9.0.0",
@@ -68,8 +68,8 @@
68
68
  "webpack-merge": "^6.0.1"
69
69
  },
70
70
  "devDependencies": {
71
- "@docusaurus/module-type-aliases": "4.0.0-canary-6972",
72
- "@docusaurus/types": "4.0.0-canary-6972",
71
+ "@docusaurus/module-type-aliases": "4.0.0-canary-6973",
72
+ "@docusaurus/types": "4.0.0-canary-6973",
73
73
  "@total-typescript/shoehorn": "^0.1.2",
74
74
  "@types/escape-html": "^1.0.4",
75
75
  "@types/history": "^4.7.11",