@brillout/docpress 0.15.10-commit-e9efbd3 → 0.15.10-commit-af2d9bc

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 (38) hide show
  1. package/components/CodeSnippets/useSelectCodeLang.ts +61 -0
  2. package/components/CodeSnippets.css +47 -0
  3. package/components/CodeSnippets.tsx +80 -45
  4. package/css/tooltip.css +10 -2
  5. package/detypePlugin.ts +73 -48
  6. package/dist/+config.js +1 -1
  7. package/dist/NavItemComponent.js +38 -46
  8. package/dist/components/CodeBlockTransformer.js +2 -3
  9. package/dist/components/CodeSnippets/useSelectCodeLang.d.ts +7 -0
  10. package/dist/components/CodeSnippets/useSelectCodeLang.js +51 -0
  11. package/dist/components/CodeSnippets.d.ts +10 -6
  12. package/dist/components/CodeSnippets.js +66 -94
  13. package/dist/components/Comment.js +1 -2
  14. package/dist/components/FileRemoved.js +4 -6
  15. package/dist/components/HorizontalLine.js +1 -2
  16. package/dist/components/ImportMeta.js +2 -3
  17. package/dist/components/Link.js +34 -50
  18. package/dist/components/Note.js +17 -29
  19. package/dist/components/P.js +1 -12
  20. package/dist/components/RepoLink.js +7 -9
  21. package/dist/determineNavItemsColumnLayout.js +48 -63
  22. package/dist/detypePlugin.js +84 -120
  23. package/dist/parseMarkdownMini.js +5 -17
  24. package/dist/parsePageSections.js +41 -82
  25. package/dist/renderer/usePageContext.js +6 -7
  26. package/dist/resolvePageContext.js +91 -103
  27. package/dist/utils/Emoji/Emoji.js +13 -21
  28. package/dist/utils/assert.js +14 -16
  29. package/dist/utils/cls.js +1 -1
  30. package/dist/utils/determineSectionUrlHash.js +5 -5
  31. package/dist/utils/filter.js +2 -2
  32. package/dist/utils/getGlobalObject.js +3 -3
  33. package/dist/vite.config.js +7 -7
  34. package/package.json +1 -1
  35. package/tsconfig.json +2 -1
  36. package/dist/utils/useSelectedLanguage.d.ts +0 -7
  37. package/dist/utils/useSelectedLanguage.js +0 -49
  38. package/utils/useSelectedLanguage.ts +0 -61
@@ -1,47 +1,25 @@
1
- var __assign = (this && this.__assign) || function () {
2
- __assign = Object.assign || function(t) {
3
- for (var s, i = 1, n = arguments.length; i < n; i++) {
4
- s = arguments[i];
5
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
- t[p] = s[p];
7
- }
8
- return t;
9
- };
10
- return __assign.apply(this, arguments);
11
- };
12
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
13
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
14
- if (ar || !(i in from)) {
15
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
16
- ar[i] = from[i];
17
- }
18
- }
19
- return to.concat(ar || Array.prototype.slice.call(from));
20
- };
21
1
  export { determineNavItemsColumnLayout };
22
2
  import { assert, assertUsage } from './utils/assert';
23
3
  function determineNavItemsColumnLayout(navItems) {
24
- var columnLayouts = getColumnEntries(navItems);
25
- columnLayouts.forEach(function (columnEntries) {
26
- var _loop_1 = function (numberOfColumns) {
27
- var columnMapping = determineColumnLayout(columnEntries.map(function (columnEntry) { return columnEntry.numberOfEntries; }), numberOfColumns);
28
- columnEntries.forEach(function (columnEntry, i) {
29
- var _a;
30
- var _b;
31
- (_a = (_b = columnEntry.navItemLeader).isColumnEntry) !== null && _a !== void 0 ? _a : (_b.isColumnEntry = {});
4
+ const columnLayouts = getColumnEntries(navItems);
5
+ columnLayouts.forEach((columnEntries) => {
6
+ for (let numberOfColumns = columnEntries.length; numberOfColumns >= 1; numberOfColumns--) {
7
+ const columnMapping = determineColumnLayout(columnEntries.map((columnEntry) => columnEntry.numberOfEntries), numberOfColumns);
8
+ columnEntries.forEach((columnEntry, i) => {
9
+ columnEntry.navItemLeader.isColumnEntry ??= {};
32
10
  columnEntry.navItemLeader.isColumnEntry[numberOfColumns] = columnMapping[i];
33
11
  });
34
- };
35
- for (var numberOfColumns = columnEntries.length; numberOfColumns >= 1; numberOfColumns--) {
36
- _loop_1(numberOfColumns);
37
12
  }
38
13
  });
39
14
  }
40
15
  function getColumnEntries(navItems) {
41
- var navItemsWithLength = navItems.map(function (navItem) { return (__assign(__assign({}, navItem), { numberOfHeadings: navItem.level === 1 || navItem.level === 4 ? 0 : null })); });
42
- var navItemLevel1;
43
- var navItemLevel4;
44
- navItemsWithLength.forEach(function (navItem) {
16
+ const navItemsWithLength = navItems.map((navItem) => ({
17
+ ...navItem,
18
+ numberOfHeadings: navItem.level === 1 || navItem.level === 4 ? 0 : null,
19
+ }));
20
+ let navItemLevel1;
21
+ let navItemLevel4;
22
+ navItemsWithLength.forEach((navItem) => {
45
23
  if (navItem.level === 1) {
46
24
  navItemLevel1 = navItem;
47
25
  navItemLevel4 = undefined;
@@ -51,7 +29,7 @@ function getColumnEntries(navItems) {
51
29
  navItemLevel4 = navItem;
52
30
  return;
53
31
  }
54
- var bumpNavItemLength = function (navItem) {
32
+ const bumpNavItemLength = (navItem) => {
55
33
  assert(navItem.numberOfHeadings !== null);
56
34
  navItem.numberOfHeadings++;
57
35
  };
@@ -61,13 +39,13 @@ function getColumnEntries(navItems) {
61
39
  bumpNavItemLength(navItemLevel4);
62
40
  }
63
41
  });
64
- var columnLayouts = [];
65
- var columnEntries = [];
66
- var isFullWidthCategory;
67
- navItemsWithLength.forEach(function (navItem, i) {
68
- var isFullWidthCategoryBegin = false;
42
+ const columnLayouts = [];
43
+ let columnEntries = [];
44
+ let isFullWidthCategory;
45
+ navItemsWithLength.forEach((navItem, i) => {
46
+ let isFullWidthCategoryBegin = false;
69
47
  if (navItem.level === 1) {
70
- var isFullWidthCategoryPrevious = isFullWidthCategory;
48
+ const isFullWidthCategoryPrevious = isFullWidthCategory;
71
49
  isFullWidthCategory = !!navItem.menuModalFullWidth;
72
50
  if (isFullWidthCategory)
73
51
  isFullWidthCategoryBegin = true;
@@ -76,8 +54,8 @@ function getColumnEntries(navItems) {
76
54
  columnEntries = [];
77
55
  }
78
56
  }
79
- var navItemPrevious = navItemsWithLength[i - 1];
80
- var navItemNext = navItemsWithLength[i + 1];
57
+ const navItemPrevious = navItemsWithLength[i - 1];
58
+ const navItemNext = navItemsWithLength[i + 1];
81
59
  if (!isFullWidthCategory
82
60
  ? navItem.level === 1
83
61
  : (navItem.level === 4 && navItemPrevious.level !== 1) || isFullWidthCategoryBegin) {
@@ -87,7 +65,7 @@ function getColumnEntries(navItems) {
87
65
  else {
88
66
  assert(navItem.level === 1);
89
67
  }
90
- var numberOfHeadings = navItem.numberOfHeadings;
68
+ let { numberOfHeadings } = navItem;
91
69
  assert(numberOfHeadings !== null);
92
70
  if (isFullWidthCategoryBegin) {
93
71
  assert(navItem.level === 1);
@@ -106,15 +84,15 @@ function getColumnEntries(navItems) {
106
84
  }
107
85
  function determineColumnLayout(columnsUnmerged, numberOfColumns) {
108
86
  assert(numberOfColumns <= columnsUnmerged.length);
109
- var columnsMergingInit = columnsUnmerged.map(function (columnHeight, i) { return ({
87
+ const columnsMergingInit = columnsUnmerged.map((columnHeight, i) => ({
110
88
  columnIdsMerged: [i],
111
89
  heightTotal: columnHeight,
112
- }); });
113
- var columnsMerged = mergeColumns(columnsMergingInit, numberOfColumns);
114
- var columnsIdMap = new Array(columnsUnmerged.length);
90
+ }));
91
+ const columnsMerged = mergeColumns(columnsMergingInit, numberOfColumns);
92
+ const columnsIdMap = new Array(columnsUnmerged.length);
115
93
  assert(columnsMerged.length === numberOfColumns);
116
- columnsMerged.forEach(function (columnMerged, columnMergedId) {
117
- columnMerged.columnIdsMerged.forEach(function (columnId) {
94
+ columnsMerged.forEach((columnMerged, columnMergedId) => {
95
+ columnMerged.columnIdsMerged.forEach((columnId) => {
118
96
  columnsIdMap[columnId] = columnMergedId;
119
97
  });
120
98
  });
@@ -124,25 +102,32 @@ function determineColumnLayout(columnsUnmerged, numberOfColumns) {
124
102
  function mergeColumns(columnsMerging, numberOfColumns) {
125
103
  if (columnsMerging.length <= numberOfColumns)
126
104
  return columnsMerging;
127
- var mergeCandidate = null;
128
- for (var i_1 = 0; i_1 <= columnsMerging.length - 2; i_1++) {
129
- var column1 = columnsMerging[i_1 + 0];
130
- var column2 = columnsMerging[i_1 + 1];
131
- var heightTotal = column1.heightTotal + column2.heightTotal;
105
+ let mergeCandidate = null;
106
+ for (let i = 0; i <= columnsMerging.length - 2; i++) {
107
+ const column1 = columnsMerging[i + 0];
108
+ const column2 = columnsMerging[i + 1];
109
+ const heightTotal = column1.heightTotal + column2.heightTotal;
132
110
  if (!mergeCandidate || mergeCandidate.heightTotal > heightTotal) {
133
111
  mergeCandidate = {
134
- i: i_1,
135
- columnIdsMerged: __spreadArray(__spreadArray([], column1.columnIdsMerged, true), column2.columnIdsMerged, true),
136
- heightTotal: heightTotal,
112
+ i,
113
+ columnIdsMerged: [
114
+ //
115
+ ...column1.columnIdsMerged,
116
+ ...column2.columnIdsMerged,
117
+ ],
118
+ heightTotal,
137
119
  };
138
120
  }
139
121
  }
140
122
  assert(mergeCandidate);
141
- var i = mergeCandidate.i;
123
+ const { i } = mergeCandidate;
142
124
  assert(-1 < i && i < columnsMerging.length - 1);
143
- var columnsMergingMod = __spreadArray(__spreadArray(__spreadArray([], columnsMerging.slice(0, i), true), [
144
- mergeCandidate
145
- ], false), columnsMerging.slice(i + 2), true);
125
+ const columnsMergingMod = [
126
+ //
127
+ ...columnsMerging.slice(0, i),
128
+ mergeCandidate,
129
+ ...columnsMerging.slice(i + 2),
130
+ ];
146
131
  assert(columnsMergingMod.length === columnsMerging.length - 1);
147
132
  return mergeColumns(columnsMergingMod, numberOfColumns);
148
133
  }
@@ -1,140 +1,104 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
- var __generator = (this && this.__generator) || function (thisArg, body) {
11
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
12
- return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13
- function verb(n) { return function (v) { return step([n, v]); }; }
14
- function step(op) {
15
- if (f) throw new TypeError("Generator is already executing.");
16
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
17
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
18
- if (y = 0, t) op = [op[0] & 2, t.value];
19
- switch (op[0]) {
20
- case 0: case 1: t = op; break;
21
- case 4: _.label++; return { value: op[1], done: false };
22
- case 5: _.label++; y = op[1]; op = [0]; continue;
23
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
24
- default:
25
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29
- if (t[2]) _.ops.pop();
30
- _.trys.pop(); continue;
31
- }
32
- op = body.call(thisArg, _);
33
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35
- }
36
- };
37
1
  export { detypePlugin };
38
2
  import module from 'node:module';
3
+ import { assertUsage } from './utils/assert.js';
4
+ import pc from '@brillout/picocolors';
39
5
  // Cannot use `import { transform } from 'detype'` as it results in errors,
40
6
  // and the package has no default export. Using `module.createRequire` instead.
41
- var transform = module.createRequire(import.meta.url)('detype').transform;
7
+ const { transform: detype } = module.createRequire(import.meta.url)('detype');
8
+ const prettierOptions = {
9
+ semi: false,
10
+ singleQuote: true,
11
+ printWidth: 100,
12
+ trailingComma: 'none',
13
+ };
14
+ // RegExp to find TypeScript code blocks.
15
+ //
16
+ // For example:
17
+ // ~~~mdx
18
+ // ```ts
19
+ // const hello: string = 'world'
20
+ // ```
21
+ // ~~~
22
+ //
23
+ // But also indented code blocks:
24
+ // ~~~mdx
25
+ // > Also works:
26
+ // > - In blockquotes
27
+ // > - In bullet points
28
+ // > ```ts
29
+ // > const hello: string = 'world'
30
+ // > ```
31
+ // ~~~
32
+ const codeBlockRE = /^(.*)```(tsx?|vue)[^\n]*\n([\s\S]*?)```/gm;
42
33
  function detypePlugin() {
43
- var _this = this;
44
34
  return {
45
35
  name: '@brillout/docpress:detypePlugin',
46
36
  enforce: 'pre',
47
- transform: function (code, id) { return __awaiter(_this, void 0, void 0, function () {
48
- var codeNew;
49
- return __generator(this, function (_a) {
50
- switch (_a.label) {
51
- case 0:
52
- if (!id.endsWith('+Page.mdx')) {
53
- return [2 /*return*/];
54
- }
55
- return [4 /*yield*/, transformCode(code)];
56
- case 1:
57
- codeNew = _a.sent();
58
- return [2 /*return*/, codeNew];
59
- }
60
- });
61
- }); },
37
+ transform: async (code, moduleId) => {
38
+ if (!moduleId.endsWith('.mdx'))
39
+ return;
40
+ const codeNew = await transformCode(code, moduleId);
41
+ return codeNew;
42
+ },
62
43
  };
63
44
  }
64
- var codeBlockRE = /^([ \t]{0,3}>?[ \t]?)```(tsx?|vue)[^\n]*\n([\s\S]*?)```/gm;
65
- var prettierOptions = {
66
- semi: false,
67
- singleQuote: true,
68
- printWidth: 100,
69
- };
70
- function transformCode(code) {
71
- return __awaiter(this, void 0, void 0, function () {
72
- var matches, codeNew, lastIndex, _i, matches_1, match, fullMatch, startsWith, lang, tsCode, tsOpeningCode, blockStart, blockEnd, jsCode, jsLang, jsOpeningCode, closing, jsCodeSnippet, tsCodeSnippet, codeSnippets;
73
- return __generator(this, function (_a) {
74
- switch (_a.label) {
75
- case 0:
76
- matches = Array.from(code.matchAll(codeBlockRE));
77
- if (matches.length === 0) {
78
- return [2 /*return*/, code];
79
- }
80
- codeNew = "import { CodeSnippets, CodeSnippet } from '@brillout/docpress';\n\n";
81
- lastIndex = 0;
82
- _i = 0, matches_1 = matches;
83
- _a.label = 1;
84
- case 1:
85
- if (!(_i < matches_1.length)) return [3 /*break*/, 6];
86
- match = matches_1[_i];
87
- fullMatch = match[0], startsWith = match[1], lang = match[2], tsCode = match[3];
88
- tsOpeningCode = fullMatch.split('\n')[0].slice(startsWith.length);
89
- blockStart = match.index;
90
- blockEnd = blockStart + fullMatch.length;
91
- codeNew += code.slice(lastIndex, blockStart);
92
- if (startsWith.length > 0) {
93
- tsCode = stripStarts(tsCode, startsWith);
94
- }
95
- if (!tsOpeningCode.includes('ts-only')) return [3 /*break*/, 2];
96
- codeNew += "".concat(startsWith, "<CodeSnippet language={'ts'} tsOnly={'true'}>\n").concat(fullMatch, "\n").concat(startsWith, "</CodeSnippet>");
97
- return [3 /*break*/, 4];
98
- case 2: return [4 /*yield*/, transform(tsCode.replaceAll('.ts', '.js'), "tsCode.".concat(lang), {
99
- removeTsComments: true,
100
- prettierOptions: prettierOptions,
101
- })];
102
- case 3:
103
- jsCode = _a.sent();
104
- jsLang = lang === 'vue' ? 'vue' : lang.replace('t', 'j') // ts => js | tsx => jsx
105
- ;
106
- jsOpeningCode = tsOpeningCode.replace(lang, jsLang);
107
- closing = "```";
108
- jsCodeSnippet = "<CodeSnippet language={'js'}>\n".concat(jsOpeningCode, "\n").concat(jsCode).concat(closing, "\n</CodeSnippet>");
109
- tsCodeSnippet = "<CodeSnippet language={'ts'}>\n".concat(tsOpeningCode, "\n").concat(tsCode).concat(closing, "\n</CodeSnippet>");
110
- codeSnippets = putBackStarts("<CodeSnippets>\n".concat(tsCodeSnippet, "\n").concat(jsCodeSnippet, "\n</CodeSnippets>"), startsWith);
111
- codeNew += codeSnippets;
112
- _a.label = 4;
113
- case 4:
114
- lastIndex = blockEnd;
115
- _a.label = 5;
116
- case 5:
117
- _i++;
118
- return [3 /*break*/, 1];
119
- case 6:
120
- codeNew += code.slice(lastIndex);
121
- return [2 /*return*/, codeNew];
122
- }
123
- });
124
- });
45
+ async function transformCode(code, moduleId) {
46
+ const matches = Array.from(code.matchAll(codeBlockRE));
47
+ if (matches.length === 0)
48
+ return;
49
+ let codeNew = `import { CodeSnippets, CodeSnippet } from '@brillout/docpress';\n\n`;
50
+ let lastIndex = 0;
51
+ for (const match of matches) {
52
+ const [codeBlockOuterStr, codeBlockIndent, codeBlockLang, codeBlockContentWithIndent] = match;
53
+ // Remove indentation
54
+ const codeBlockOpen = codeBlockOuterStr.split('\n')[0].slice(codeBlockIndent.length);
55
+ const codeBlockContent = removeCodeBlockIndent(codeBlockContentWithIndent, codeBlockIndent, moduleId);
56
+ const blockStartIndex = match.index;
57
+ const blockEndIndex = blockStartIndex + codeBlockOuterStr.length;
58
+ codeNew += code.slice(lastIndex, blockStartIndex);
59
+ if (codeBlockOpen.includes('ts-only')) {
60
+ codeNew += `${codeBlockIndent}<CodeSnippet codeLang="ts" tsOnly>\n${codeBlockOuterStr}\n${codeBlockIndent}</CodeSnippet>`;
61
+ }
62
+ else {
63
+ // someFileName.ts => someFileName.js
64
+ let codeBlockContentJs = codeBlockContent.replaceAll('.ts', '.js');
65
+ // Remove TypeScript
66
+ codeBlockContentJs = await detype(codeBlockContentJs, `some-dummy-filename.${codeBlockLang}`, {
67
+ removeTsComments: true,
68
+ prettierOptions,
69
+ });
70
+ const codeBlockLangJs = codeBlockLang === 'vue'
71
+ ? 'vue'
72
+ : // ts => js | tsx => jsx
73
+ codeBlockLang.replace('t', 'j');
74
+ const codeBlockOpenJs = codeBlockOpen.replace(codeBlockLang, codeBlockLangJs);
75
+ const codeBlockClose = '```';
76
+ const codeSnippetTs = `<CodeSnippet codeLang="ts">\n${codeBlockOpen}\n${codeBlockContent}${codeBlockClose}\n</CodeSnippet>`;
77
+ const codeSnippetJs = `<CodeSnippet codeLang="js">\n${codeBlockOpenJs}\n${codeBlockContentJs}${codeBlockClose}\n</CodeSnippet>`;
78
+ const codeSnippets = restoreCodeBlockIndent(`<CodeSnippets>\n${codeSnippetJs}\n${codeSnippetTs}\n</CodeSnippets>`, codeBlockIndent);
79
+ codeNew += codeSnippets;
80
+ }
81
+ lastIndex = blockEndIndex;
82
+ }
83
+ codeNew += code.slice(lastIndex);
84
+ return codeNew;
125
85
  }
126
- function stripStarts(code, startsWith) {
86
+ function removeCodeBlockIndent(code, codeBlockIndent, moduleId) {
87
+ if (!codeBlockIndent.length)
88
+ return code;
127
89
  return code
128
90
  .split('\n')
129
- .map(function (line) { return line.slice(startsWith.length); })
91
+ .map((line) => {
92
+ assertUsage(line.startsWith(codeBlockIndent.trimEnd()), `In ${pc.bold(pc.blue(moduleId))} the line ${pc.bold(line)} must start with ${pc.bold(codeBlockIndent)}`);
93
+ return line.slice(codeBlockIndent.length);
94
+ })
130
95
  .join('\n');
131
96
  }
132
- function putBackStarts(code, startsWith) {
133
- if (!startsWith.length) {
97
+ function restoreCodeBlockIndent(code, codeBlockIndent) {
98
+ if (!codeBlockIndent.length)
134
99
  return code;
135
- }
136
100
  return code
137
101
  .split('\n')
138
- .map(function (line) { return "".concat(startsWith).concat(line); })
102
+ .map((line) => `${codeBlockIndent}${line}`)
139
103
  .join('\n');
140
104
  }
@@ -1,20 +1,11 @@
1
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
2
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
3
- if (ar || !(i in from)) {
4
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
5
- ar[i] = from[i];
6
- }
7
- }
8
- return to.concat(ar || Array.prototype.slice.call(from));
9
- };
10
1
  export { parseMarkdownMini };
11
2
  import React from 'react';
12
3
  function parseMarkdownMini(markdown) {
13
- var parts = [];
14
- var current;
15
- markdown.split('').forEach(function (letter) {
4
+ const parts = [];
5
+ let current;
6
+ markdown.split('').forEach((letter) => {
16
7
  if (letter === '`') {
17
- if ((current === null || current === void 0 ? void 0 : current.nodeType) === 'code') {
8
+ if (current?.nodeType === 'code') {
18
9
  // </code>
19
10
  parts.push(current);
20
11
  current = undefined;
@@ -37,9 +28,6 @@ function parseMarkdownMini(markdown) {
37
28
  if (current) {
38
29
  parts.push(current);
39
30
  }
40
- var parsed = React.createElement.apply(React, __spreadArray([React.Fragment,
41
- {}], parts.map(function (part, i) {
42
- return React.createElement(part.nodeType === 'code' ? 'code' : React.Fragment, { key: i }, part.content);
43
- }), false));
31
+ const parsed = React.createElement(React.Fragment, {}, ...parts.map((part, i) => React.createElement(part.nodeType === 'code' ? 'code' : React.Fragment, { key: i }, part.content)));
44
32
  return parsed;
45
33
  }
@@ -1,66 +1,26 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
- var __generator = (this && this.__generator) || function (thisArg, body) {
11
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
12
- return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13
- function verb(n) { return function (v) { return step([n, v]); }; }
14
- function step(op) {
15
- if (f) throw new TypeError("Generator is already executing.");
16
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
17
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
18
- if (y = 0, t) op = [op[0] & 2, t.value];
19
- switch (op[0]) {
20
- case 0: case 1: t = op; break;
21
- case 4: _.label++; return { value: op[1], done: false };
22
- case 5: _.label++; y = op[1]; op = [0]; continue;
23
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
24
- default:
25
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29
- if (t[2]) _.ops.pop();
30
- _.trys.pop(); continue;
31
- }
32
- op = body.call(thisArg, _);
33
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35
- }
36
- };
37
1
  export { parsePageSections };
38
2
  import { assert } from './utils/assert.js';
39
3
  import { determineSectionUrlHash } from './utils/determineSectionUrlHash.js';
40
4
  import os from 'os';
41
5
  function parsePageSections() {
42
- var _this = this;
43
6
  return {
44
7
  name: '@brillout/docpress:parsePageSections',
45
8
  enforce: 'pre',
46
- transform: function (code, id) { return __awaiter(_this, void 0, void 0, function () {
47
- var codeNew;
48
- return __generator(this, function (_a) {
49
- if (!id.endsWith('+Page.mdx')) {
50
- return [2 /*return*/];
51
- }
52
- codeNew = transform(code);
53
- return [2 /*return*/, codeNew];
54
- });
55
- }); },
9
+ transform: async (code, id) => {
10
+ if (!id.endsWith('+Page.mdx')) {
11
+ return;
12
+ }
13
+ const codeNew = transform(code);
14
+ return codeNew;
15
+ },
56
16
  };
57
17
  }
58
18
  function transform(code) {
59
- var pageSections = [];
60
- var isCodeBlock = false;
61
- var codeNew = code
19
+ const pageSections = [];
20
+ let isCodeBlock = false;
21
+ let codeNew = code
62
22
  .split('\n')
63
- .map(function (line) {
23
+ .map((line) => {
64
24
  // Skip code blocks, e.g.
65
25
  // ~~~md
66
26
  // # Markdown Example
@@ -78,51 +38,50 @@ function transform(code) {
78
38
  || line.startsWith('<h2')
79
39
  */
80
40
  ) {
81
- var _a = parsePageSection(line), pageSectionId = _a.pageSectionId, pageSectionLevel = _a.pageSectionLevel, pageSectionTitle = _a.pageSectionTitle, headingHtml = _a.headingHtml;
82
- pageSections.push({ pageSectionId: pageSectionId, pageSectionLevel: pageSectionLevel, pageSectionTitle: pageSectionTitle });
41
+ const { pageSectionId, pageSectionLevel, pageSectionTitle, headingHtml } = parsePageSection(line);
42
+ pageSections.push({ pageSectionId, pageSectionLevel, pageSectionTitle });
83
43
  return headingHtml;
84
44
  }
85
45
  return line;
86
46
  })
87
47
  .join('\n');
88
- var exportCode = "export const pageSectionsExport = [".concat(pageSections
89
- .map(function (pageSection) { return JSON.stringify(pageSection); })
90
- .join(', '), "];");
91
- codeNew += "\n\n".concat(exportCode, "\n");
48
+ const exportCode = `export const pageSectionsExport = [${pageSections
49
+ .map((pageSection) => JSON.stringify(pageSection))
50
+ .join(', ')}];`;
51
+ codeNew += `\n\n${exportCode}\n`;
92
52
  return codeNew;
93
53
  }
94
54
  function parsePageSection(line) {
95
- var _a;
96
- var _b = line.split(' '), lineBegin = _b[0], lineWords = _b.slice(1);
97
- assert(lineBegin.split('#').join('') === '', { line: line, lineWords: lineWords });
98
- var pageSectionLevel = lineBegin.length;
99
- var titleMdx = lineWords.join(' ');
100
- assert(!titleMdx.startsWith(' '), { line: line, lineWords: lineWords });
101
- assert(titleMdx, { line: line, lineWords: lineWords });
102
- var pageSectionTitle = titleMdx;
103
- var anchor = titleMdx;
55
+ const [lineBegin, ...lineWords] = line.split(' ');
56
+ assert(lineBegin.split('#').join('') === '', { line, lineWords });
57
+ const pageSectionLevel = lineBegin.length;
58
+ const titleMdx = lineWords.join(' ');
59
+ assert(!titleMdx.startsWith(' '), { line, lineWords });
60
+ assert(titleMdx, { line, lineWords });
61
+ let pageSectionTitle = titleMdx;
62
+ let anchor = titleMdx;
104
63
  {
105
64
  // Support custom anchor: `## Some Title{#custom-anchor}`
106
- var customAnchor = (_a = /(?<={#).*(?=})/g.exec(titleMdx)) === null || _a === void 0 ? void 0 : _a[0];
65
+ const customAnchor = /(?<={#).*(?=})/g.exec(titleMdx)?.[0];
107
66
  if (customAnchor) {
108
67
  anchor = customAnchor;
109
68
  pageSectionTitle = titleMdx.replace(/{#.*}/g, '');
110
69
  }
111
70
  }
112
- var pageSectionId = determineSectionUrlHash(anchor);
113
- var titleParsed = parseMarkdownMini(pageSectionTitle);
71
+ const pageSectionId = determineSectionUrlHash(anchor);
72
+ const titleParsed = parseMarkdownMini(pageSectionTitle);
114
73
  assert(pageSectionId === null || pageSectionId.length > 0);
115
- var headingId = pageSectionId === null ? '' : " id=\"".concat(pageSectionId, "\"");
116
- var headingHtml = "<h".concat(pageSectionLevel).concat(headingId, ">").concat(titleParsed, "</h").concat(pageSectionLevel, ">");
117
- var pageSection = { pageSectionLevel: pageSectionLevel, pageSectionTitle: pageSectionTitle, pageSectionId: pageSectionId, headingHtml: headingHtml };
74
+ const headingId = pageSectionId === null ? '' : ` id="${pageSectionId}"`;
75
+ const headingHtml = `<h${pageSectionLevel}${headingId}>${titleParsed}</h${pageSectionLevel}>`;
76
+ const pageSection = { pageSectionLevel, pageSectionTitle, pageSectionId, headingHtml };
118
77
  return pageSection;
119
78
  }
120
79
  function parseMarkdownMini(titleMarkdown) {
121
- var parts = [];
122
- var current;
123
- titleMarkdown.split('').forEach(function (letter) {
80
+ const parts = [];
81
+ let current;
82
+ titleMarkdown.split('').forEach((letter) => {
124
83
  if (letter === '`') {
125
- if ((current === null || current === void 0 ? void 0 : current.nodeType) === 'code') {
84
+ if (current?.nodeType === 'code') {
126
85
  // </code>
127
86
  parts.push(current);
128
87
  current = undefined;
@@ -145,25 +104,25 @@ function parseMarkdownMini(titleMarkdown) {
145
104
  if (current) {
146
105
  parts.push(current);
147
106
  }
148
- var titleHtml = parts
149
- .map(function (part) {
107
+ const titleHtml = parts
108
+ .map((part) => {
150
109
  if (part.nodeType === 'code') {
151
- return "<code>".concat(serializeText(part.content), "</code>");
110
+ return `<code>${serializeText(part.content)}</code>`;
152
111
  }
153
112
  else {
154
- assert(part.nodeType === 'text', { parts: parts });
113
+ assert(part.nodeType === 'text', { parts });
155
114
  return serializeText(part.content);
156
115
  }
157
116
  })
158
117
  .join('');
159
118
  return titleHtml;
160
119
  function serializeText(text) {
161
- var textEscaped = text.split("'").join("\\'");
120
+ let textEscaped = text.split("'").join("\\'");
162
121
  // https://github.com/brillout/docpress/pull/2
163
122
  if (isWindows()) {
164
123
  textEscaped = textEscaped.replace(/\r/, '');
165
124
  }
166
- return "{'".concat(textEscaped, "'}");
125
+ return `{'${textEscaped}'}`;
167
126
  }
168
127
  }
169
128
  function isWindows() {