@brillout/docpress 0.15.10-commit-ef0b9a0 → 0.15.10-commit-b6b1605

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.
@@ -1,53 +1,156 @@
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
+ };
1
37
  export { detypePlugin };
2
38
  import module from 'node:module';
39
+ import { createContentMap, contentMapKeyRE } from './utils/contentMap.js';
3
40
  // Cannot use `import { transform } from 'detype'` as it results in errors,
4
41
  // and the package has no default export. Using `module.createRequire` instead.
5
- const { transform } = module.createRequire(import.meta.url)('detype');
42
+ var transform = module.createRequire(import.meta.url)('detype').transform;
6
43
  function detypePlugin() {
44
+ var _this = this;
7
45
  return {
8
46
  name: '@brillout/docpress:detypePlugin',
9
47
  enforce: 'pre',
10
- transform: async (code, id) => {
11
- if (!id.endsWith('+Page.mdx')) {
12
- return;
13
- }
14
- const codeNew = await transformCode(code);
15
- return codeNew;
16
- },
48
+ transform: function (code, id) { return __awaiter(_this, void 0, void 0, function () {
49
+ var contentMap, codeNew, replaced;
50
+ return __generator(this, function (_a) {
51
+ switch (_a.label) {
52
+ case 0:
53
+ if (!id.endsWith('+Page.mdx')) {
54
+ return [2 /*return*/];
55
+ }
56
+ contentMap = createContentMap();
57
+ return [4 /*yield*/, transformCode(code, contentMap)];
58
+ case 1:
59
+ codeNew = _a.sent();
60
+ replaced = replaceContent(codeNew, contentMapKeyRE, function (match) {
61
+ var content = contentMap.get(match[0]);
62
+ if (!content) {
63
+ throw new Error('Content not found');
64
+ }
65
+ return content;
66
+ });
67
+ return [2 /*return*/, replaced];
68
+ }
69
+ });
70
+ }); },
17
71
  };
18
72
  }
19
- const tsBlockRegex = /```(tsx?|vue)(\s+ts-only)?([\s\S]*?)```/g;
20
- async function transformCode(code) {
21
- let codeNew = `import { CodeSnippets, CodeSnippet } from '@brillout/docpress';\n`;
22
- let lastIndex = 0;
23
- const matches = [...code.matchAll(tsBlockRegex)];
24
- if (matches.length === 0) {
73
+ var codeBlockRE = /^([ \t]{0,3}>?[ \t]?)```(tsx?|vue)[^\n]*\n([\s\S]*?)```/gm;
74
+ var prettierOptions = {
75
+ semi: false,
76
+ singleQuote: true,
77
+ printWidth: 120,
78
+ };
79
+ function transformCode(code, contentMap) {
80
+ return __awaiter(this, void 0, void 0, function () {
81
+ var matches, codeNew, lastIndex, _i, matches_1, match, fullMatch, startsWith, lang, tsCode, tsOpeningCode, blockStart, blockEnd, key, jsCode, jsLang, jsOpeningCode, closing, jsCodeSnippet, tsCodeSnippet, codeSnippets, key;
82
+ return __generator(this, function (_a) {
83
+ switch (_a.label) {
84
+ case 0:
85
+ matches = Array.from(code.matchAll(codeBlockRE));
86
+ if (matches.length === 0) {
87
+ return [2 /*return*/, code];
88
+ }
89
+ codeNew = "import { CodeSnippets, CodeSnippet } from '@brillout/docpress';\n";
90
+ lastIndex = 0;
91
+ _i = 0, matches_1 = matches;
92
+ _a.label = 1;
93
+ case 1:
94
+ if (!(_i < matches_1.length)) return [3 /*break*/, 6];
95
+ match = matches_1[_i];
96
+ fullMatch = match[0], startsWith = match[1], lang = match[2], tsCode = match[3];
97
+ tsOpeningCode = fullMatch.split('\n')[0].slice(startsWith.length);
98
+ blockStart = match.index;
99
+ blockEnd = blockStart + fullMatch.length;
100
+ codeNew += code.slice(lastIndex, blockStart);
101
+ if (startsWith.length > 0) {
102
+ tsCode = stripStarts(tsCode, startsWith);
103
+ }
104
+ if (!tsOpeningCode.includes('ts-only')) return [3 /*break*/, 2];
105
+ key = contentMap.add('ts-code-snippet', fullMatch.length, fullMatch);
106
+ codeNew += "".concat(startsWith, "<CodeSnippet language={'ts'} tsOnly={'true'}>\n").concat(key, "\n").concat(startsWith, "</CodeSnippet>");
107
+ return [3 /*break*/, 4];
108
+ case 2: return [4 /*yield*/, transform(tsCode.replaceAll('.ts', '.js'), "tsCode.".concat(lang), {
109
+ removeTsComments: true,
110
+ prettierOptions: prettierOptions,
111
+ })];
112
+ case 3:
113
+ jsCode = _a.sent();
114
+ jsLang = lang === 'vue' ? 'vue' : lang.replace('t', 'j') // ts => js | tsx => jsx
115
+ ;
116
+ jsOpeningCode = tsOpeningCode.replace(lang, jsLang);
117
+ closing = "```";
118
+ jsCodeSnippet = "<CodeSnippet language={'js'}>\n".concat(jsOpeningCode, "\n").concat(jsCode).concat(closing, "\n</CodeSnippet>");
119
+ tsCodeSnippet = "<CodeSnippet language={'ts'}>\n".concat(tsOpeningCode, "\n").concat(tsCode).concat(closing, "\n</CodeSnippet>");
120
+ codeSnippets = putBackStarts("".concat(tsCodeSnippet, "\n").concat(jsCodeSnippet), startsWith);
121
+ key = contentMap.add("ts-js-code-snippets", codeSnippets.length, codeSnippets);
122
+ codeNew += "".concat(startsWith, "<CodeSnippets>\n").concat(key, "\n").concat(startsWith, "</CodeSnippets>");
123
+ _a.label = 4;
124
+ case 4:
125
+ lastIndex = blockEnd;
126
+ _a.label = 5;
127
+ case 5:
128
+ _i++;
129
+ return [3 /*break*/, 1];
130
+ case 6:
131
+ codeNew += code.slice(lastIndex);
132
+ return [2 /*return*/, codeNew];
133
+ }
134
+ });
135
+ });
136
+ }
137
+ function stripStarts(code, startsWith) {
138
+ return code
139
+ .split('\n')
140
+ .map(function (line) { return line.slice(startsWith.length); })
141
+ .join('\n');
142
+ }
143
+ function putBackStarts(code, startsWith) {
144
+ if (!startsWith.length) {
25
145
  return code;
26
146
  }
27
- for (const match of matches) {
28
- const [tsCodeBlock, lang, tsOnly, tsCode] = match; // lang = ts | tsx | vue
29
- const type = lang === 'vue' ? 'vue' : lang.replace('t', 'j'); // ts => js | tsx => jsx
30
- const blockStart = match.index;
31
- const blockEnd = blockStart + tsCodeBlock.length;
32
- codeNew += code.slice(lastIndex, blockStart);
33
- if (tsOnly) {
34
- codeNew += `\n<CodeSnippet language={'ts'} tsOnly={'true'}>\n${tsCodeBlock}\n</CodeSnippet>\n`;
35
- }
36
- else {
37
- const jsCode = await transform(tsCode.trim().replaceAll('.ts', '.js'), `tsCode.${lang}`, {
38
- removeTsComments: true,
39
- prettierOptions: {
40
- semi: false,
41
- singleQuote: true,
42
- },
43
- });
44
- const jsCodeBlock = `\`\`\`${type}\n${jsCode}\`\`\``;
45
- const jsCodeSnippet = `\n<CodeSnippet language={'js'}>\n${jsCodeBlock}\n</CodeSnippet>\n`;
46
- const tsCodeSnippet = `\n<CodeSnippet language={'ts'}>\n${tsCodeBlock}\n</CodeSnippet>\n`;
47
- codeNew += `<CodeSnippets>${jsCodeSnippet}${tsCodeSnippet}</CodeSnippets>`;
48
- }
49
- lastIndex = blockEnd;
50
- }
51
- codeNew += code.slice(lastIndex);
52
- return codeNew;
147
+ return code
148
+ .split('\n')
149
+ .map(function (line) { return "".concat(startsWith).concat(line); })
150
+ .join('\n');
151
+ }
152
+ function replaceContent(input, re, replacer) {
153
+ var replacements = Array.from(input.matchAll(re), replacer);
154
+ var i = 0;
155
+ return input.replace(re, function () { return replacements[i++]; });
53
156
  }
@@ -1,11 +1,20 @@
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
+ };
1
10
  export { parseMarkdownMini };
2
11
  import React from 'react';
3
12
  function parseMarkdownMini(markdown) {
4
- const parts = [];
5
- let current;
6
- markdown.split('').forEach((letter) => {
13
+ var parts = [];
14
+ var current;
15
+ markdown.split('').forEach(function (letter) {
7
16
  if (letter === '`') {
8
- if (current?.nodeType === 'code') {
17
+ if ((current === null || current === void 0 ? void 0 : current.nodeType) === 'code') {
9
18
  // </code>
10
19
  parts.push(current);
11
20
  current = undefined;
@@ -28,6 +37,9 @@ function parseMarkdownMini(markdown) {
28
37
  if (current) {
29
38
  parts.push(current);
30
39
  }
31
- const parsed = React.createElement(React.Fragment, {}, ...parts.map((part, i) => React.createElement(part.nodeType === 'code' ? 'code' : React.Fragment, { key: i }, part.content)));
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));
32
44
  return parsed;
33
45
  }
@@ -1,26 +1,66 @@
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
+ };
1
37
  export { parsePageSections };
2
38
  import { assert } from './utils/assert.js';
3
39
  import { determineSectionUrlHash } from './utils/determineSectionUrlHash.js';
4
40
  import os from 'os';
5
41
  function parsePageSections() {
42
+ var _this = this;
6
43
  return {
7
44
  name: '@brillout/docpress:parsePageSections',
8
45
  enforce: 'pre',
9
- transform: async (code, id) => {
10
- if (!id.endsWith('+Page.mdx')) {
11
- return;
12
- }
13
- const codeNew = transform(code);
14
- return codeNew;
15
- },
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
+ }); },
16
56
  };
17
57
  }
18
58
  function transform(code) {
19
- const pageSections = [];
20
- let isCodeBlock = false;
21
- let codeNew = code
59
+ var pageSections = [];
60
+ var isCodeBlock = false;
61
+ var codeNew = code
22
62
  .split('\n')
23
- .map((line) => {
63
+ .map(function (line) {
24
64
  // Skip code blocks, e.g.
25
65
  // ~~~md
26
66
  // # Markdown Example
@@ -38,50 +78,51 @@ function transform(code) {
38
78
  || line.startsWith('<h2')
39
79
  */
40
80
  ) {
41
- const { pageSectionId, pageSectionLevel, pageSectionTitle, headingHtml } = parsePageSection(line);
42
- pageSections.push({ pageSectionId, pageSectionLevel, pageSectionTitle });
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 });
43
83
  return headingHtml;
44
84
  }
45
85
  return line;
46
86
  })
47
87
  .join('\n');
48
- const exportCode = `export const pageSectionsExport = [${pageSections
49
- .map((pageSection) => JSON.stringify(pageSection))
50
- .join(', ')}];`;
51
- codeNew += `\n\n${exportCode}\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");
52
92
  return codeNew;
53
93
  }
54
94
  function parsePageSection(line) {
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;
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;
63
104
  {
64
105
  // Support custom anchor: `## Some Title{#custom-anchor}`
65
- const customAnchor = /(?<={#).*(?=})/g.exec(titleMdx)?.[0];
106
+ var customAnchor = (_a = /(?<={#).*(?=})/g.exec(titleMdx)) === null || _a === void 0 ? void 0 : _a[0];
66
107
  if (customAnchor) {
67
108
  anchor = customAnchor;
68
109
  pageSectionTitle = titleMdx.replace(/{#.*}/g, '');
69
110
  }
70
111
  }
71
- const pageSectionId = determineSectionUrlHash(anchor);
72
- const titleParsed = parseMarkdownMini(pageSectionTitle);
112
+ var pageSectionId = determineSectionUrlHash(anchor);
113
+ var titleParsed = parseMarkdownMini(pageSectionTitle);
73
114
  assert(pageSectionId === null || pageSectionId.length > 0);
74
- const headingId = pageSectionId === null ? '' : ` id="${pageSectionId}"`;
75
- const headingHtml = `<h${pageSectionLevel}${headingId}>${titleParsed}</h${pageSectionLevel}>`;
76
- const pageSection = { pageSectionLevel, pageSectionTitle, pageSectionId, headingHtml };
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 };
77
118
  return pageSection;
78
119
  }
79
120
  function parseMarkdownMini(titleMarkdown) {
80
- const parts = [];
81
- let current;
82
- titleMarkdown.split('').forEach((letter) => {
121
+ var parts = [];
122
+ var current;
123
+ titleMarkdown.split('').forEach(function (letter) {
83
124
  if (letter === '`') {
84
- if (current?.nodeType === 'code') {
125
+ if ((current === null || current === void 0 ? void 0 : current.nodeType) === 'code') {
85
126
  // </code>
86
127
  parts.push(current);
87
128
  current = undefined;
@@ -104,25 +145,25 @@ function parseMarkdownMini(titleMarkdown) {
104
145
  if (current) {
105
146
  parts.push(current);
106
147
  }
107
- const titleHtml = parts
108
- .map((part) => {
148
+ var titleHtml = parts
149
+ .map(function (part) {
109
150
  if (part.nodeType === 'code') {
110
- return `<code>${serializeText(part.content)}</code>`;
151
+ return "<code>".concat(serializeText(part.content), "</code>");
111
152
  }
112
153
  else {
113
- assert(part.nodeType === 'text', { parts });
154
+ assert(part.nodeType === 'text', { parts: parts });
114
155
  return serializeText(part.content);
115
156
  }
116
157
  })
117
158
  .join('');
118
159
  return titleHtml;
119
160
  function serializeText(text) {
120
- let textEscaped = text.split("'").join("\\'");
161
+ var textEscaped = text.split("'").join("\\'");
121
162
  // https://github.com/brillout/docpress/pull/2
122
163
  if (isWindows()) {
123
164
  textEscaped = textEscaped.replace(/\r/, '');
124
165
  }
125
- return `{'${textEscaped}'}`;
166
+ return "{'".concat(textEscaped, "'}");
126
167
  }
127
168
  }
128
169
  function isWindows() {
@@ -3,19 +3,20 @@ export { usePageContext };
3
3
  export { usePageContextLegacy };
4
4
  import React, { useContext } from 'react';
5
5
  import { getGlobalObject } from '../utils/getGlobalObject';
6
- const globalObject = getGlobalObject('usePageContext.ts', {
6
+ var globalObject = getGlobalObject('usePageContext.ts', {
7
7
  Ctx: React.createContext(undefined),
8
8
  });
9
9
  function usePageContextLegacy() {
10
- const { Ctx } = globalObject;
11
- const pageContext = useContext(Ctx);
10
+ var Ctx = globalObject.Ctx;
11
+ var pageContext = useContext(Ctx);
12
12
  return pageContext.resolved;
13
13
  }
14
14
  function usePageContext() {
15
- const pageContext = useContext(globalObject.Ctx);
15
+ var pageContext = useContext(globalObject.Ctx);
16
16
  return pageContext;
17
17
  }
18
- function PageContextProvider({ pageContext, children, }) {
19
- const { Ctx } = globalObject;
18
+ function PageContextProvider(_a) {
19
+ var pageContext = _a.pageContext, children = _a.children;
20
+ var Ctx = globalObject.Ctx;
20
21
  return React.createElement(Ctx.Provider, { value: pageContext }, children);
21
22
  }