@docusaurus/core 2.0.0-beta.15 → 2.0.0-beta.16

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 (131) hide show
  1. package/bin/beforeCli.mjs +136 -0
  2. package/bin/{docusaurus.js → docusaurus.mjs} +62 -40
  3. package/lib/babel/preset.d.ts +1 -2
  4. package/lib/babel/preset.js +5 -4
  5. package/lib/choosePort.js +22 -30
  6. package/lib/client/App.d.ts +1 -2
  7. package/lib/client/App.js +13 -8
  8. package/lib/client/LinksCollector.js +1 -1
  9. package/lib/client/PendingNavigation.d.ts +4 -4
  10. package/lib/client/PendingNavigation.js +4 -6
  11. package/lib/client/baseUrlIssueBanner/BaseUrlIssueBanner.d.ts +8 -0
  12. package/lib/client/baseUrlIssueBanner/BaseUrlIssueBanner.js +15 -10
  13. package/lib/client/client-lifecycles-dispatcher.d.ts +2 -5
  14. package/lib/client/client-lifecycles-dispatcher.js +5 -7
  15. package/lib/client/clientEntry.js +11 -5
  16. package/lib/client/docusaurus.js +6 -4
  17. package/lib/client/exports/BrowserOnly.d.ts +1 -2
  18. package/lib/client/exports/BrowserOnly.js +2 -3
  19. package/lib/client/exports/ComponentCreator.d.ts +1 -2
  20. package/lib/client/exports/ComponentCreator.js +7 -6
  21. package/lib/client/exports/ErrorBoundary.d.ts +2 -2
  22. package/lib/client/exports/ErrorBoundary.js +1 -2
  23. package/lib/client/exports/Head.d.ts +2 -3
  24. package/lib/client/exports/Head.js +3 -4
  25. package/lib/client/exports/Interpolate.js +9 -12
  26. package/lib/client/exports/Link.d.ts +11 -5
  27. package/lib/client/exports/Link.js +13 -7
  28. package/lib/client/exports/Translate.js +2 -1
  29. package/lib/client/exports/browserContext.js +3 -2
  30. package/lib/client/exports/docusaurusContext.js +1 -1
  31. package/lib/client/exports/isInternalUrl.js +1 -1
  32. package/lib/client/exports/renderRoutes.d.ts +1 -2
  33. package/lib/client/exports/renderRoutes.js +1 -2
  34. package/lib/client/exports/router.d.ts +1 -1
  35. package/lib/client/exports/router.js +1 -1
  36. package/lib/client/exports/useDocusaurusContext.d.ts +1 -2
  37. package/lib/client/exports/useDocusaurusContext.js +1 -2
  38. package/lib/client/flat.d.ts +1 -2
  39. package/lib/client/flat.js +1 -2
  40. package/lib/client/normalizeLocation.d.ts +2 -3
  41. package/lib/client/normalizeLocation.js +1 -2
  42. package/lib/client/prefetch.d.ts +1 -2
  43. package/lib/client/prefetch.js +1 -2
  44. package/lib/client/preload.d.ts +2 -1
  45. package/lib/client/preload.js +2 -1
  46. package/lib/client/serverEntry.js +23 -19
  47. package/lib/client/theme-fallback/Error/index.d.ts +10 -0
  48. package/lib/client/theme-fallback/Error/index.js +21 -29
  49. package/lib/client/theme-fallback/Layout/index.d.ts +10 -0
  50. package/lib/client/theme-fallback/Layout/index.js +10 -19
  51. package/lib/client/theme-fallback/Loading/index.d.ts +9 -0
  52. package/lib/client/theme-fallback/Loading/index.js +46 -114
  53. package/lib/{server/versions/__tests/index.test.d.ts → client/theme-fallback/NotFound/index.d.ts} +2 -1
  54. package/lib/client/theme-fallback/NotFound/index.js +9 -16
  55. package/lib/client/theme-fallback/Root/index.d.ts +10 -0
  56. package/lib/client/theme-fallback/Root/index.js +2 -5
  57. package/lib/commands/build.js +33 -34
  58. package/lib/commands/clear.js +23 -11
  59. package/lib/commands/deploy.js +12 -11
  60. package/lib/commands/external.d.ts +2 -2
  61. package/lib/commands/external.js +1 -1
  62. package/lib/commands/serve.js +3 -2
  63. package/lib/commands/start.js +4 -4
  64. package/lib/commands/swizzle/actions.d.ts +23 -0
  65. package/lib/commands/swizzle/actions.js +102 -0
  66. package/lib/commands/swizzle/common.d.ts +33 -0
  67. package/lib/commands/swizzle/common.js +57 -0
  68. package/lib/commands/swizzle/components.d.ts +29 -0
  69. package/lib/commands/swizzle/components.js +165 -0
  70. package/lib/commands/swizzle/config.d.ts +10 -0
  71. package/lib/commands/swizzle/config.js +77 -0
  72. package/lib/commands/swizzle/context.d.ts +8 -0
  73. package/lib/commands/swizzle/context.js +30 -0
  74. package/lib/commands/swizzle/index.d.ts +8 -0
  75. package/lib/commands/swizzle/index.js +115 -0
  76. package/lib/commands/swizzle/prompts.d.ts +12 -0
  77. package/lib/commands/swizzle/prompts.js +110 -0
  78. package/lib/commands/swizzle/tables.d.ts +9 -0
  79. package/lib/commands/swizzle/tables.js +116 -0
  80. package/lib/commands/swizzle/themes.d.ts +20 -0
  81. package/lib/commands/swizzle/themes.js +105 -0
  82. package/lib/commands/writeHeadingIds.d.ts +1 -1
  83. package/lib/commands/writeHeadingIds.js +13 -14
  84. package/lib/commands/writeTranslations.js +10 -7
  85. package/lib/index.d.ts +10 -9
  86. package/lib/index.js +20 -19
  87. package/lib/server/brokenLinks.js +30 -20
  88. package/lib/server/config.js +1 -1
  89. package/lib/server/configValidation.d.ts +1 -1
  90. package/lib/server/configValidation.js +32 -23
  91. package/lib/server/duplicateRoutes.js +2 -4
  92. package/lib/server/html-tags/htmlTags.js +1 -2
  93. package/lib/server/i18n.d.ts +0 -1
  94. package/lib/server/i18n.js +16 -26
  95. package/lib/server/index.d.ts +1 -1
  96. package/lib/server/index.js +17 -15
  97. package/lib/server/loadSetup.d.ts +1 -2
  98. package/lib/server/loadSetup.js +2 -2
  99. package/lib/server/moduleShorthand.js +1 -1
  100. package/lib/server/plugins/index.js +9 -9
  101. package/lib/server/plugins/init.d.ts +11 -1
  102. package/lib/server/plugins/init.js +23 -28
  103. package/lib/server/plugins/pluginIds.js +4 -3
  104. package/lib/server/presets/index.d.ts +2 -2
  105. package/lib/server/presets/index.js +3 -3
  106. package/lib/server/routes.js +13 -7
  107. package/lib/server/themes/alias.d.ts +1 -1
  108. package/lib/server/themes/alias.js +5 -6
  109. package/lib/server/themes/index.d.ts +2 -2
  110. package/lib/server/themes/index.js +10 -9
  111. package/lib/server/translations/translations.js +10 -11
  112. package/lib/server/translations/translationsExtractor.js +20 -19
  113. package/lib/server/versions/index.d.ts +2 -3
  114. package/lib/server/versions/index.js +22 -21
  115. package/lib/webpack/base.d.ts +2 -2
  116. package/lib/webpack/base.js +30 -22
  117. package/lib/webpack/client.d.ts +1 -1
  118. package/lib/webpack/client.js +7 -4
  119. package/lib/webpack/plugins/ChunkAssetPlugin.d.ts +12 -2
  120. package/lib/webpack/plugins/ChunkAssetPlugin.js +17 -10
  121. package/lib/webpack/plugins/CleanWebpackPlugin.d.ts +5 -5
  122. package/lib/webpack/plugins/CleanWebpackPlugin.js +5 -4
  123. package/lib/webpack/server.d.ts +1 -1
  124. package/lib/webpack/server.js +6 -5
  125. package/lib/webpack/utils.d.ts +3 -3
  126. package/lib/webpack/utils.js +17 -37
  127. package/package.json +56 -56
  128. package/bin/beforeCli.js +0 -124
  129. package/lib/commands/swizzle.d.ts +0 -9
  130. package/lib/commands/swizzle.js +0 -236
  131. package/lib/server/versions/__tests/index.test.js +0 -26
@@ -0,0 +1,115 @@
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 logger_1 = (0, tslib_1.__importDefault)(require("@docusaurus/logger"));
11
+ const themes_1 = require("./themes");
12
+ const components_1 = require("./components");
13
+ const tables_1 = require("./tables");
14
+ const common_1 = require("./common");
15
+ const actions_1 = require("./actions");
16
+ const config_1 = require("./config");
17
+ const prompts_1 = require("./prompts");
18
+ const context_1 = require("./context");
19
+ async function listAllThemeComponents({ themeNames, plugins, typescript, }) {
20
+ const themeComponentsTables = (await Promise.all(themeNames.map(async (themeName) => {
21
+ const themePath = (0, themes_1.getThemePath)({ themeName, plugins, typescript });
22
+ const swizzleConfig = (0, config_1.getThemeSwizzleConfig)(themeName, plugins);
23
+ const themeComponents = await (0, components_1.getThemeComponents)({
24
+ themeName,
25
+ themePath,
26
+ swizzleConfig,
27
+ });
28
+ return (0, tables_1.themeComponentsTable)(themeComponents);
29
+ }))).join('\n\n');
30
+ logger_1.default.info(`All theme components available to swizzle:
31
+
32
+ ${themeComponentsTables}
33
+
34
+ ${(0, tables_1.helpTables)()}
35
+ `);
36
+ return process.exit(0);
37
+ }
38
+ async function ensureActionSafety({ componentName, componentConfig, action, danger, }) {
39
+ const actionStatus = componentConfig.actions[action];
40
+ if (actionStatus === 'forbidden') {
41
+ logger_1.default.error `
42
+ Swizzle action name=${action} is forbidden for component name=${componentName}
43
+ `;
44
+ return process.exit(1);
45
+ }
46
+ if (actionStatus === 'unsafe' && !danger) {
47
+ logger_1.default.warn `
48
+ Swizzle action name=${action} is unsafe to perform on name=${componentName}.
49
+ It is more likely to be affected by breaking changes in the future
50
+ If you want to swizzle it, use the code=${'--danger'} flag, or confirm that you understand the risks.
51
+ `;
52
+ const swizzleDangerousComponent = await (0, prompts_1.askSwizzleDangerousComponent)();
53
+ if (!swizzleDangerousComponent) {
54
+ return process.exit(1);
55
+ }
56
+ }
57
+ return undefined;
58
+ }
59
+ async function swizzle(siteDir, themeNameParam, componentNameParam, optionsParam) {
60
+ const options = (0, common_1.normalizeOptions)(optionsParam);
61
+ const { list, danger, typescript } = options;
62
+ const { plugins } = await (0, context_1.initSwizzleContext)(siteDir);
63
+ const themeNames = (0, themes_1.getThemeNames)(plugins);
64
+ if (list && !themeNameParam) {
65
+ await listAllThemeComponents({ themeNames, plugins, typescript });
66
+ }
67
+ const themeName = await (0, themes_1.getThemeName)({ themeNameParam, themeNames, list });
68
+ const themePath = (0, themes_1.getThemePath)({ themeName, plugins, typescript });
69
+ const swizzleConfig = (0, config_1.getThemeSwizzleConfig)(themeName, plugins);
70
+ const themeComponents = await (0, components_1.getThemeComponents)({
71
+ themeName,
72
+ themePath,
73
+ swizzleConfig,
74
+ });
75
+ const componentName = await (0, components_1.getComponentName)({
76
+ componentNameParam,
77
+ themeComponents,
78
+ list,
79
+ });
80
+ const componentConfig = themeComponents.getConfig(componentName);
81
+ const action = await (0, actions_1.getAction)(componentConfig, options);
82
+ await ensureActionSafety({ componentName, componentConfig, action, danger });
83
+ async function executeAction() {
84
+ switch (action) {
85
+ case 'wrap': {
86
+ const result = await (0, actions_1.wrap)({
87
+ siteDir,
88
+ themePath,
89
+ componentName,
90
+ typescript,
91
+ });
92
+ logger_1.default.success `
93
+ Created wrapper of name=${componentName} from name=${themeName} in path=${result.createdFiles}.
94
+ `;
95
+ return result;
96
+ }
97
+ case 'eject': {
98
+ const result = await (0, actions_1.eject)({
99
+ siteDir,
100
+ themePath,
101
+ componentName,
102
+ });
103
+ logger_1.default.success `
104
+ Ejected name=${componentName} from name=${themeName} to path=${result.createdFiles}.
105
+ `;
106
+ return result;
107
+ }
108
+ default:
109
+ throw new Error(`Unexpected action ${action}`);
110
+ }
111
+ }
112
+ await executeAction();
113
+ return process.exit(0);
114
+ }
115
+ exports.default = swizzle;
@@ -0,0 +1,12 @@
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 { ThemeComponents } from './components';
8
+ import type { SwizzleAction, SwizzleComponentConfig } from '@docusaurus/types';
9
+ export declare function askThemeName(themeNames: string[]): Promise<string>;
10
+ export declare function askComponentName(themeComponents: ThemeComponents): Promise<string>;
11
+ export declare function askSwizzleDangerousComponent(): Promise<boolean>;
12
+ export declare function askSwizzleAction(componentConfig: SwizzleComponentConfig): Promise<SwizzleAction>;
@@ -0,0 +1,110 @@
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.askSwizzleAction = exports.askSwizzleDangerousComponent = exports.askComponentName = exports.askThemeName = void 0;
10
+ const tslib_1 = require("tslib");
11
+ const logger_1 = (0, tslib_1.__importDefault)(require("@docusaurus/logger"));
12
+ const prompts_1 = (0, tslib_1.__importDefault)(require("prompts"));
13
+ const common_1 = require("./common");
14
+ const ExitTitle = logger_1.default.yellow('[Exit]');
15
+ async function askThemeName(themeNames) {
16
+ const { themeName } = await (0, prompts_1.default)({
17
+ type: 'select',
18
+ name: 'themeName',
19
+ message: 'Select a theme to swizzle:',
20
+ choices: themeNames
21
+ .map((theme) => ({ title: theme, value: theme }))
22
+ .concat({ title: ExitTitle, value: '[Exit]' }),
23
+ });
24
+ if (!themeName || themeName === '[Exit]') {
25
+ process.exit(0);
26
+ }
27
+ return themeName;
28
+ }
29
+ exports.askThemeName = askThemeName;
30
+ async function askComponentName(themeComponents) {
31
+ function formatComponentName(componentName) {
32
+ const anySafe = themeComponents.hasAnySafeAction(componentName);
33
+ const allSafe = themeComponents.hasAllSafeAction(componentName);
34
+ const safestStatus = anySafe ? 'safe' : 'unsafe'; // Not 100% accurate but good enough for now.
35
+ const partiallySafe = anySafe && !allSafe;
36
+ return `${componentName}${(0, common_1.actionStatusSuffix)(safestStatus, {
37
+ partiallySafe,
38
+ })}`;
39
+ }
40
+ const { componentName } = await (0, prompts_1.default)({
41
+ type: 'autocomplete',
42
+ name: 'componentName',
43
+ message: `
44
+ Select or type the component to swizzle.
45
+ ${common_1.PartiallySafeHint} = not safe for all swizzle actions
46
+ `,
47
+ // This doesn't work well in small-height terminals (like IDE)
48
+ // limit: 30,
49
+ // This does not work well and messes up with terminal scroll position
50
+ // limit: Number.POSITIVE_INFINITY,
51
+ choices: themeComponents.all
52
+ .map((compName) => ({
53
+ title: formatComponentName(compName),
54
+ value: compName,
55
+ }))
56
+ .concat({ title: ExitTitle, value: '[Exit]' }),
57
+ async suggest(input, choices) {
58
+ return choices.filter((choice) => choice.title.toLowerCase().includes(input.toLowerCase()));
59
+ },
60
+ });
61
+ logger_1.default.newLine();
62
+ if (!componentName || componentName === '[Exit]') {
63
+ return process.exit(0);
64
+ }
65
+ return componentName;
66
+ }
67
+ exports.askComponentName = askComponentName;
68
+ async function askSwizzleDangerousComponent() {
69
+ const { switchToDanger } = await (0, prompts_1.default)({
70
+ type: 'select',
71
+ name: 'switchToDanger',
72
+ message: `Do you really want to swizzle this unsafe internal component?`,
73
+ choices: [
74
+ { title: logger_1.default.green('NO: cancel and stay safe'), value: false },
75
+ {
76
+ title: logger_1.default.red('YES: I know what I am doing!'),
77
+ value: true,
78
+ },
79
+ { title: ExitTitle, value: '[Exit]' },
80
+ ],
81
+ });
82
+ if (typeof switchToDanger === 'undefined' || switchToDanger === '[Exit]') {
83
+ return process.exit(0);
84
+ }
85
+ return !!switchToDanger;
86
+ }
87
+ exports.askSwizzleDangerousComponent = askSwizzleDangerousComponent;
88
+ async function askSwizzleAction(componentConfig) {
89
+ const { action } = await (0, prompts_1.default)({
90
+ type: 'select',
91
+ name: 'action',
92
+ message: `Which swizzle action do you want to do?`,
93
+ choices: [
94
+ {
95
+ title: `${logger_1.default.bold('Wrap')}${(0, common_1.actionStatusSuffix)(componentConfig.actions.wrap)}`,
96
+ value: 'wrap',
97
+ },
98
+ {
99
+ title: `${logger_1.default.bold('Eject')}${(0, common_1.actionStatusSuffix)(componentConfig.actions.eject)}`,
100
+ value: 'eject',
101
+ },
102
+ { title: ExitTitle, value: '[Exit]' },
103
+ ],
104
+ });
105
+ if (typeof action === 'undefined' || action === '[Exit]') {
106
+ return process.exit(0);
107
+ }
108
+ return action;
109
+ }
110
+ exports.askSwizzleAction = askSwizzleAction;
@@ -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 { ThemeComponents } from './components';
8
+ export declare function helpTables(): string;
9
+ export declare function themeComponentsTable(themeComponents: ThemeComponents): string;
@@ -0,0 +1,116 @@
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.themeComponentsTable = exports.helpTables = void 0;
10
+ const tslib_1 = require("tslib");
11
+ const logger_1 = (0, tslib_1.__importDefault)(require("@docusaurus/logger"));
12
+ const cli_table3_1 = (0, tslib_1.__importDefault)(require("cli-table3"));
13
+ const lodash_1 = (0, tslib_1.__importDefault)(require("lodash"));
14
+ const actions_1 = require("./actions");
15
+ const common_1 = require("./common");
16
+ function tableStatusLabel(status) {
17
+ return (0, common_1.actionStatusColor)(status, (0, common_1.actionStatusLabel)(status));
18
+ }
19
+ function getStatusLabel(status) {
20
+ return (0, common_1.actionStatusColor)(status, (0, common_1.actionStatusLabel)(status));
21
+ }
22
+ function statusTable() {
23
+ const table = new cli_table3_1.default({
24
+ head: ['Status', 'CLI option', 'Description'],
25
+ });
26
+ table.push({
27
+ [tableStatusLabel('safe')]: [
28
+ '',
29
+ `
30
+ This component is safe to swizzle and was designed for this purpose.
31
+ The swizzled component is retro-compatible with minor version upgrades.
32
+ `,
33
+ ],
34
+ });
35
+ table.push({
36
+ [tableStatusLabel('unsafe')]: [
37
+ logger_1.default.code('--danger'),
38
+ `
39
+ This component is unsafe to swizzle, but you can still do it!
40
+ Warning: we may release breaking changes within minor version upgrades.
41
+ You will have to upgrade your component manually and maintain it over time.
42
+
43
+ ${logger_1.default.green('Tip')}: your customization can't be done in a ${tableStatusLabel('safe')} way?
44
+ Report it here: https://github.com/facebook/docusaurus/discussions/5468
45
+ `,
46
+ ],
47
+ });
48
+ table.push({
49
+ [tableStatusLabel('forbidden')]: [
50
+ '',
51
+ `
52
+ This component should not meant to be swizzled.
53
+ `,
54
+ ],
55
+ });
56
+ return table.toString();
57
+ }
58
+ function actionsTable() {
59
+ const table = new cli_table3_1.default({
60
+ head: ['Actions', 'CLI option', 'Description'],
61
+ });
62
+ table.push({
63
+ [logger_1.default.bold('Wrap')]: [
64
+ logger_1.default.code('--wrap'),
65
+ `
66
+ Creates a wrapper around the original theme component.
67
+ Allows rendering other components before/after the original theme component.
68
+
69
+ ${logger_1.default.green('Tip')}: prefer ${logger_1.default.code('--wrap')} whenever possible to reduces the amount of code to maintain.
70
+ `,
71
+ ],
72
+ });
73
+ table.push({
74
+ [logger_1.default.bold('Eject')]: [
75
+ logger_1.default.code('--eject'),
76
+ `
77
+ Ejects the full source code of the original theme component.
78
+ Allows overriding the original component entirely with your own UI and logic.
79
+
80
+ ${logger_1.default.green('Tip')}: ${logger_1.default.code('--eject')} can be useful to completely redesign a component.
81
+ `,
82
+ ],
83
+ });
84
+ return table.toString();
85
+ }
86
+ function helpTables() {
87
+ return `${logger_1.default.bold('Swizzle actions')}:
88
+ ${actionsTable()}
89
+
90
+ ${logger_1.default.bold('Swizzle safety statuses')}:
91
+ ${statusTable()}
92
+
93
+ ${logger_1.default.bold('Swizzle guide')}: https://docusaurus.io/docs/swizzling`;
94
+ }
95
+ exports.helpTables = helpTables;
96
+ function themeComponentsTable(themeComponents) {
97
+ const table = new cli_table3_1.default({
98
+ head: [
99
+ 'Component name',
100
+ ...actions_1.SwizzleActions.map((action) => lodash_1.default.capitalize(action)),
101
+ 'Description',
102
+ ],
103
+ });
104
+ themeComponents.all.forEach((component) => {
105
+ table.push({
106
+ [component]: [
107
+ ...actions_1.SwizzleActions.map((action) => getStatusLabel(themeComponents.getActionStatus(component, action))),
108
+ themeComponents.getDescription(component),
109
+ ],
110
+ });
111
+ });
112
+ return `${logger_1.default.bold(`Components available for swizzle in ${logger_1.default.name(themeComponents.themeName)}`)}:
113
+ ${table.toString()}
114
+ `;
115
+ }
116
+ exports.themeComponentsTable = themeComponentsTable;
@@ -0,0 +1,20 @@
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 SwizzlePlugin } from './common';
8
+ export declare function pluginToThemeName(plugin: SwizzlePlugin): string | undefined;
9
+ export declare function getPluginByThemeName(plugins: SwizzlePlugin[], themeName: string): SwizzlePlugin;
10
+ export declare function getThemeNames(plugins: SwizzlePlugin[]): string[];
11
+ export declare function getThemeName({ themeNameParam, themeNames, list, }: {
12
+ themeNameParam: string | undefined;
13
+ themeNames: string[];
14
+ list: boolean | undefined;
15
+ }): Promise<string>;
16
+ export declare function getThemePath({ plugins, themeName, typescript, }: {
17
+ plugins: SwizzlePlugin[];
18
+ themeName: string;
19
+ typescript: boolean | undefined;
20
+ }): string;
@@ -0,0 +1,105 @@
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.getThemePath = exports.getThemeName = exports.getThemeNames = exports.getPluginByThemeName = exports.pluginToThemeName = void 0;
10
+ const tslib_1 = require("tslib");
11
+ const logger_1 = (0, tslib_1.__importDefault)(require("@docusaurus/logger"));
12
+ const leven_1 = (0, tslib_1.__importDefault)(require("leven"));
13
+ const lodash_1 = (0, tslib_1.__importDefault)(require("lodash"));
14
+ const prompts_1 = require("./prompts");
15
+ const common_1 = require("./common");
16
+ function pluginToThemeName(plugin) {
17
+ var _a;
18
+ if (plugin.instance.getThemePath) {
19
+ return ((_a = plugin.instance.version.name) !== null && _a !== void 0 ? _a : plugin.instance.name);
20
+ }
21
+ return undefined;
22
+ }
23
+ exports.pluginToThemeName = pluginToThemeName;
24
+ function getPluginByThemeName(plugins, themeName) {
25
+ const plugin = plugins.find((p) => pluginToThemeName(p) === themeName);
26
+ if (!plugin) {
27
+ throw new Error(`Theme ${themeName} not found`);
28
+ }
29
+ return plugin;
30
+ }
31
+ exports.getPluginByThemeName = getPluginByThemeName;
32
+ function getThemeNames(plugins) {
33
+ const themeNames = lodash_1.default.uniq(
34
+ // The fact that getThemePath is attached to the plugin instance makes
35
+ // this code impossible to optimize. If this is a static method, we don't
36
+ // need to initialize all plugins just to filter which are themes
37
+ // Benchmark: loadContext-58ms; initPlugins-323ms
38
+ plugins.map((plugin) => pluginToThemeName(plugin)).filter(Boolean));
39
+ // Opinionated ordering: user is most likely to swizzle:
40
+ // - the classic theme
41
+ // - official themes
42
+ // - official plugins
43
+ return lodash_1.default.orderBy(themeNames, [
44
+ (t) => t === '@docusaurus/theme-classic',
45
+ (t) => t.includes('@docusaurus/theme'),
46
+ (t) => t.includes('@docusaurus'),
47
+ ], ['desc', 'desc', 'desc']);
48
+ }
49
+ exports.getThemeNames = getThemeNames;
50
+ // Returns a valid value if recovering is possible
51
+ function handleInvalidThemeName({ themeNameParam, themeNames, }) {
52
+ // Trying to recover invalid value
53
+ // We look for potential matches that only differ in casing.
54
+ const differentCaseMatch = (0, common_1.findStringIgnoringCase)(themeNameParam, themeNames);
55
+ if (differentCaseMatch) {
56
+ logger_1.default.warn `Theme name=${themeNameParam} doesn't exist.`;
57
+ logger_1.default.info `name=${differentCaseMatch} will be used instead of name=${themeNameParam}.`;
58
+ return differentCaseMatch;
59
+ }
60
+ // TODO recover from short theme-names here: "classic" => "@docusaurus/theme-classic"
61
+ // No recovery value is possible: print error
62
+ const suggestion = themeNames.find((name) => (0, leven_1.default)(name, themeNameParam) < 4);
63
+ logger_1.default.error `Theme name=${themeNameParam} not found. ${suggestion
64
+ ? logger_1.default.interpolate `Did you mean name=${suggestion}?`
65
+ : logger_1.default.interpolate `Themes available for swizzle: ${themeNames}`}`;
66
+ return process.exit(1);
67
+ }
68
+ async function validateThemeName({ themeNameParam, themeNames, }) {
69
+ const isValidName = themeNames.includes(themeNameParam);
70
+ if (!isValidName) {
71
+ return handleInvalidThemeName({
72
+ themeNameParam,
73
+ themeNames,
74
+ });
75
+ }
76
+ return themeNameParam;
77
+ }
78
+ async function getThemeName({ themeNameParam, themeNames, list, }) {
79
+ if (list && !themeNameParam) {
80
+ logger_1.default.info `Themes available for swizzle: name=${themeNames}`;
81
+ return process.exit(0);
82
+ }
83
+ return themeNameParam
84
+ ? validateThemeName({ themeNameParam, themeNames })
85
+ : (0, prompts_1.askThemeName)(themeNames);
86
+ }
87
+ exports.getThemeName = getThemeName;
88
+ function getThemePath({ plugins, themeName, typescript, }) {
89
+ var _a, _b, _c, _d;
90
+ const pluginInstance = getPluginByThemeName(plugins, themeName);
91
+ const themePath = typescript
92
+ ? (_b = (_a = pluginInstance.instance).getTypeScriptThemePath) === null || _b === void 0 ? void 0 : _b.call(_a)
93
+ : (_d = (_c = pluginInstance.instance).getThemePath) === null || _d === void 0 ? void 0 : _d.call(_c);
94
+ if (!themePath) {
95
+ logger_1.default.warn(typescript
96
+ ? logger_1.default.interpolate `name=${themeName} does not provide TypeScript theme code via ${'getTypeScriptThemePath()'}.`
97
+ : // This is... technically possible to happen, e.g. returning undefined
98
+ // from getThemePath. Plugins may intentionally or unintentionally
99
+ // disguise as themes?
100
+ logger_1.default.interpolate `name=${themeName} does not provide any theme code.`);
101
+ return process.exit(1);
102
+ }
103
+ return themePath;
104
+ }
105
+ exports.getThemePath = getThemePath;
@@ -11,5 +11,5 @@ declare type Options = {
11
11
  };
12
12
  export declare function transformMarkdownHeadingLine(line: string, slugger: Slugger, options?: Options): string;
13
13
  export declare function transformMarkdownContent(content: string, options?: Options): string;
14
- export default function writeHeadingIds(siteDir: string, files?: string, options?: Options): Promise<void>;
14
+ export default function writeHeadingIds(siteDir: string, files?: string[], options?: Options): Promise<void>;
15
15
  export {};
@@ -15,7 +15,7 @@ const init_1 = (0, tslib_1.__importDefault)(require("../server/plugins/init"));
15
15
  const utils_1 = require("@docusaurus/utils");
16
16
  const utils_2 = require("../server/utils");
17
17
  function unwrapMarkdownLinks(line) {
18
- return line.replace(/\[([^\]]+)\]\([^)]+\)/g, (match, p1) => p1);
18
+ return line.replace(/\[(?<alt>[^\]]+)\]\([^)]+\)/g, (match, p1) => p1);
19
19
  }
20
20
  function addHeadingId(line, slugger, maintainCase) {
21
21
  let headingLevel = 0;
@@ -48,9 +48,7 @@ function transformMarkdownLine(line, slugger, options) {
48
48
  if (line.startsWith('##')) {
49
49
  return transformMarkdownHeadingLine(line, slugger, options);
50
50
  }
51
- else {
52
- return line;
53
- }
51
+ return line;
54
52
  }
55
53
  function transformMarkdownLines(lines, options) {
56
54
  let inCode = false;
@@ -60,12 +58,10 @@ function transformMarkdownLines(lines, options) {
60
58
  inCode = !inCode;
61
59
  return line;
62
60
  }
63
- else {
64
- if (inCode) {
65
- return line;
66
- }
67
- return transformMarkdownLine(line, slugger, options);
61
+ if (inCode) {
62
+ return line;
68
63
  }
64
+ return transformMarkdownLine(line, slugger, options);
69
65
  });
70
66
  }
71
67
  function transformMarkdownContent(content, options) {
@@ -81,12 +77,15 @@ async function transformMarkdownFile(filepath, options) {
81
77
  }
82
78
  return undefined;
83
79
  }
84
- // We only handle the "paths to watch" because these are the paths where the markdown files are
85
- // Also we don't want to transform the site md docs that do not belong to a content plugin
86
- // For example ./README.md should not be transformed
80
+ /**
81
+ * We only handle the "paths to watch" because these are the paths where the
82
+ * markdown files are. Also we don't want to transform the site md docs that do
83
+ * not belong to a content plugin. For example ./README.md should not be
84
+ * transformed
85
+ */
87
86
  async function getPathsToWatch(siteDir) {
88
87
  const context = await (0, server_1.loadContext)(siteDir);
89
- const pluginConfigs = (0, server_1.loadPluginConfigs)(context);
88
+ const pluginConfigs = await (0, server_1.loadPluginConfigs)(context);
90
89
  const plugins = await (0, init_1.default)({
91
90
  pluginConfigs,
92
91
  context,
@@ -94,7 +93,7 @@ async function getPathsToWatch(siteDir) {
94
93
  return plugins.flatMap((plugin) => { var _a, _b; return (_b = (_a = plugin === null || plugin === void 0 ? void 0 : plugin.getPathsToWatch) === null || _a === void 0 ? void 0 : _a.call(plugin)) !== null && _b !== void 0 ? _b : []; });
95
94
  }
96
95
  async function writeHeadingIds(siteDir, files, options) {
97
- const markdownFiles = await (0, utils_2.safeGlobby)(files ? [files] : await getPathsToWatch(siteDir), {
96
+ const markdownFiles = await (0, utils_2.safeGlobby)(files !== null && files !== void 0 ? files : (await getPathsToWatch(siteDir)), {
98
97
  expandDirectories: ['**/*.{md,mdx}'],
99
98
  });
100
99
  const result = await Promise.all(markdownFiles.map((p) => transformMarkdownFile(p, options)));
@@ -13,16 +13,19 @@ const init_1 = (0, tslib_1.__importDefault)(require("../server/plugins/init"));
13
13
  const translations_1 = require("../server/translations/translations");
14
14
  const translationsExtractor_1 = require("../server/translations/translationsExtractor");
15
15
  const utils_1 = require("../webpack/utils");
16
- // This is a hack, so that @docusaurus/theme-common translations are extracted!
17
- // A theme doesn't have a way to express that one of its dependency (like @docusaurus/theme-common) also has translations to extract
18
- // Instead of introducing a new lifecycle (like plugin.getThemeTranslationPaths() ?)
19
- // We just make an exception and assume that Docusaurus user is using an official theme
16
+ /**
17
+ * This is a hack, so that @docusaurus/theme-common translations are extracted!
18
+ * A theme doesn't have a way to express that one of its dependency (like
19
+ * @docusaurus/theme-common) also has translations to extract.
20
+ * Instead of introducing a new lifecycle (like `getThemeTranslationPaths()`?)
21
+ * We just make an exception and assume that user is using an official theme
22
+ */
20
23
  async function getExtraSourceCodeFilePaths() {
21
24
  try {
22
25
  const themeCommonSourceDir = path_1.default.dirname(require.resolve('@docusaurus/theme-common/lib'));
23
26
  return (0, translationsExtractor_1.globSourceCodeFilePaths)([themeCommonSourceDir]);
24
27
  }
25
- catch (e) {
28
+ catch {
26
29
  return []; // User may not use a Docusaurus official theme? Quite unlikely...
27
30
  }
28
31
  }
@@ -50,7 +53,7 @@ async function writeTranslations(siteDir, options) {
50
53
  customConfigFilePath: options.config,
51
54
  locale: options.locale,
52
55
  });
53
- const pluginConfigs = (0, server_1.loadPluginConfigs)(context);
56
+ const pluginConfigs = await (0, server_1.loadPluginConfigs)(context);
54
57
  const plugins = await (0, init_1.default)({
55
58
  pluginConfigs,
56
59
  context,
@@ -62,7 +65,7 @@ Available locales are: ${context.i18n.locales.join(',')}.`);
62
65
  }
63
66
  const babelOptions = (0, utils_1.getBabelOptions)({
64
67
  isServer: true,
65
- babelOptions: (0, utils_1.getCustomBabelConfigFilePath)(siteDir),
68
+ babelOptions: await (0, utils_1.getCustomBabelConfigFilePath)(siteDir),
66
69
  });
67
70
  const extractedCodeTranslations = await (0, translationsExtractor_1.extractSiteSourceCodeTranslations)(siteDir, plugins, babelOptions, await getExtraSourceCodeFilePaths());
68
71
  const defaultCodeMessages = await (0, translations_1.getPluginsDefaultCodeTranslationMessages)(plugins);
package/lib/index.d.ts CHANGED
@@ -4,12 +4,13 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
- export { default as build } from './commands/build';
8
- export { default as start } from './commands/start';
9
- export { default as swizzle } from './commands/swizzle';
10
- export { default as deploy } from './commands/deploy';
11
- export { default as externalCommand } from './commands/external';
12
- export { default as serve } from './commands/serve';
13
- export { default as clear } from './commands/clear';
14
- export { default as writeTranslations } from './commands/writeTranslations';
15
- export { default as writeHeadingIds } from './commands/writeHeadingIds';
7
+ import build from './commands/build';
8
+ import clear from './commands/clear';
9
+ import deploy from './commands/deploy';
10
+ import externalCommand from './commands/external';
11
+ import serve from './commands/serve';
12
+ import start from './commands/start';
13
+ import swizzle from './commands/swizzle';
14
+ import writeHeadingIds from './commands/writeHeadingIds';
15
+ import writeTranslations from './commands/writeTranslations';
16
+ export { build, clear, deploy, externalCommand, serve, start, swizzle, writeHeadingIds, writeTranslations, };