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

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.
package/detypePlugin.ts CHANGED
@@ -2,7 +2,6 @@ export { detypePlugin }
2
2
 
3
3
  import type { PluginOption } from 'vite'
4
4
  import module from 'node:module'
5
- import { createContentMap, contentMapKeyRE, type ContentMap } from './utils/contentMap.js'
6
5
 
7
6
  // Cannot use `import { transform } from 'detype'` as it results in errors,
8
7
  // and the package has no default export. Using `module.createRequire` instead.
@@ -16,17 +15,9 @@ function detypePlugin(): PluginOption {
16
15
  if (!id.endsWith('+Page.mdx')) {
17
16
  return
18
17
  }
19
- const contentMap = createContentMap()
20
- const codeNew = await transformCode(code, contentMap)
21
- const replaced = replaceContent(codeNew, contentMapKeyRE, (match) => {
22
- const content = contentMap.get(match[0])
23
- if (!content) {
24
- throw new Error('Content not found')
25
- }
26
- return content
27
- })
18
+ const codeNew = await transformCode(code)
28
19
 
29
- return replaced
20
+ return codeNew
30
21
  },
31
22
  }
32
23
  }
@@ -35,16 +26,16 @@ const codeBlockRE = /^([ \t]{0,3}>?[ \t]?)```(tsx?|vue)[^\n]*\n([\s\S]*?)```/gm
35
26
  const prettierOptions = {
36
27
  semi: false,
37
28
  singleQuote: true,
38
- printWidth: 120,
29
+ printWidth: 100,
39
30
  }
40
31
 
41
- async function transformCode(code: string, contentMap: ContentMap) {
32
+ async function transformCode(code: string) {
42
33
  const matches = Array.from(code.matchAll(codeBlockRE))
43
34
  if (matches.length === 0) {
44
35
  return code
45
36
  }
46
37
 
47
- let codeNew = `import { CodeSnippets, CodeSnippet } from '@brillout/docpress';\n`
38
+ let codeNew = `import { CodeSnippets, CodeSnippet } from '@brillout/docpress';\n\n`
48
39
  let lastIndex = 0
49
40
 
50
41
  for (const match of matches) {
@@ -61,8 +52,7 @@ async function transformCode(code: string, contentMap: ContentMap) {
61
52
  }
62
53
 
63
54
  if (tsOpeningCode.includes('ts-only')) {
64
- const key = contentMap.add('ts-code-snippet', fullMatch.length, fullMatch)
65
- codeNew += `${startsWith}<CodeSnippet language={'ts'} tsOnly={'true'}>\n${key}\n${startsWith}</CodeSnippet>`
55
+ codeNew += `${startsWith}<CodeSnippet language={'ts'} tsOnly={'true'}>\n${fullMatch}\n${startsWith}</CodeSnippet>`
66
56
  } else {
67
57
  const jsCode = await transform(tsCode.replaceAll('.ts', '.js'), `tsCode.${lang}`, {
68
58
  removeTsComments: true,
@@ -74,10 +64,12 @@ async function transformCode(code: string, contentMap: ContentMap) {
74
64
 
75
65
  const jsCodeSnippet = `<CodeSnippet language={'js'}>\n${jsOpeningCode}\n${jsCode}${closing}\n</CodeSnippet>`
76
66
  const tsCodeSnippet = `<CodeSnippet language={'ts'}>\n${tsOpeningCode}\n${tsCode}${closing}\n</CodeSnippet>`
77
- const codeSnippets = putBackStarts(`${tsCodeSnippet}\n${jsCodeSnippet}`, startsWith)
67
+ const codeSnippets = putBackStarts(
68
+ `<CodeSnippets>\n${tsCodeSnippet}\n${jsCodeSnippet}\n</CodeSnippets>`,
69
+ startsWith,
70
+ )
78
71
 
79
- const key = contentMap.add(`ts-js-code-snippets`, codeSnippets.length, codeSnippets)
80
- codeNew += `${startsWith}<CodeSnippets>\n${key}\n${startsWith}</CodeSnippets>`
72
+ codeNew += codeSnippets
81
73
  }
82
74
 
83
75
  lastIndex = blockEnd
@@ -103,10 +95,3 @@ function putBackStarts(code: string, startsWith: string) {
103
95
  .map((line) => `${startsWith}${line}`)
104
96
  .join('\n')
105
97
  }
106
-
107
- function replaceContent(input: string, re: RegExp, replacer: (match: RegExpMatchArray) => string): string {
108
- const replacements = Array.from(input.matchAll(re), replacer)
109
- let i = 0
110
-
111
- return input.replace(re, () => replacements[i++])
112
- }
@@ -36,7 +36,6 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
36
36
  };
37
37
  export { detypePlugin };
38
38
  import module from 'node:module';
39
- import { createContentMap, contentMapKeyRE } from './utils/contentMap.js';
40
39
  // Cannot use `import { transform } from 'detype'` as it results in errors,
41
40
  // and the package has no default export. Using `module.createRequire` instead.
42
41
  var transform = module.createRequire(import.meta.url)('detype').transform;
@@ -46,25 +45,17 @@ function detypePlugin() {
46
45
  name: '@brillout/docpress:detypePlugin',
47
46
  enforce: 'pre',
48
47
  transform: function (code, id) { return __awaiter(_this, void 0, void 0, function () {
49
- var contentMap, codeNew, replaced;
48
+ var codeNew;
50
49
  return __generator(this, function (_a) {
51
50
  switch (_a.label) {
52
51
  case 0:
53
52
  if (!id.endsWith('+Page.mdx')) {
54
53
  return [2 /*return*/];
55
54
  }
56
- contentMap = createContentMap();
57
- return [4 /*yield*/, transformCode(code, contentMap)];
55
+ return [4 /*yield*/, transformCode(code)];
58
56
  case 1:
59
57
  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];
58
+ return [2 /*return*/, codeNew];
68
59
  }
69
60
  });
70
61
  }); },
@@ -74,11 +65,11 @@ var codeBlockRE = /^([ \t]{0,3}>?[ \t]?)```(tsx?|vue)[^\n]*\n([\s\S]*?)```/gm;
74
65
  var prettierOptions = {
75
66
  semi: false,
76
67
  singleQuote: true,
77
- printWidth: 120,
68
+ printWidth: 100,
78
69
  };
79
- function transformCode(code, contentMap) {
70
+ function transformCode(code) {
80
71
  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;
72
+ var matches, codeNew, lastIndex, _i, matches_1, match, fullMatch, startsWith, lang, tsCode, tsOpeningCode, blockStart, blockEnd, jsCode, jsLang, jsOpeningCode, closing, jsCodeSnippet, tsCodeSnippet, codeSnippets;
82
73
  return __generator(this, function (_a) {
83
74
  switch (_a.label) {
84
75
  case 0:
@@ -86,7 +77,7 @@ function transformCode(code, contentMap) {
86
77
  if (matches.length === 0) {
87
78
  return [2 /*return*/, code];
88
79
  }
89
- codeNew = "import { CodeSnippets, CodeSnippet } from '@brillout/docpress';\n";
80
+ codeNew = "import { CodeSnippets, CodeSnippet } from '@brillout/docpress';\n\n";
90
81
  lastIndex = 0;
91
82
  _i = 0, matches_1 = matches;
92
83
  _a.label = 1;
@@ -102,8 +93,7 @@ function transformCode(code, contentMap) {
102
93
  tsCode = stripStarts(tsCode, startsWith);
103
94
  }
104
95
  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>");
96
+ codeNew += "".concat(startsWith, "<CodeSnippet language={'ts'} tsOnly={'true'}>\n").concat(fullMatch, "\n").concat(startsWith, "</CodeSnippet>");
107
97
  return [3 /*break*/, 4];
108
98
  case 2: return [4 /*yield*/, transform(tsCode.replaceAll('.ts', '.js'), "tsCode.".concat(lang), {
109
99
  removeTsComments: true,
@@ -117,9 +107,8 @@ function transformCode(code, contentMap) {
117
107
  closing = "```";
118
108
  jsCodeSnippet = "<CodeSnippet language={'js'}>\n".concat(jsOpeningCode, "\n").concat(jsCode).concat(closing, "\n</CodeSnippet>");
119
109
  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>");
110
+ codeSnippets = putBackStarts("<CodeSnippets>\n".concat(tsCodeSnippet, "\n").concat(jsCodeSnippet, "\n</CodeSnippets>"), startsWith);
111
+ codeNew += codeSnippets;
123
112
  _a.label = 4;
124
113
  case 4:
125
114
  lastIndex = blockEnd;
@@ -149,8 +138,3 @@ function putBackStarts(code, startsWith) {
149
138
  .map(function (line) { return "".concat(startsWith).concat(line); })
150
139
  .join('\n');
151
140
  }
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++]; });
156
- }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@brillout/docpress",
3
- "version": "0.15.10-commit-b6b1605",
3
+ "version": "0.15.10-commit-e9efbd3",
4
4
  "type": "module",
5
5
  "dependencies": {
6
6
  "@brillout/picocolors": "^1.0.10",
@@ -1,9 +0,0 @@
1
- export type ContentMap = {
2
- /**
3
- * @returns key
4
- */
5
- add(title: string, sourceLength: number, content: string): string;
6
- get(key: string): string | undefined;
7
- };
8
- export declare const createContentMap: () => ContentMap;
9
- export declare const contentMapKeyRE: RegExp;
@@ -1,22 +0,0 @@
1
- import { createHash } from 'node:crypto';
2
- export var createContentMap = function () {
3
- var map = new Map();
4
- return {
5
- add: function (title, length, content) {
6
- var key = generateKey("".concat(title, "_").concat(length));
7
- if (!map.has(key)) {
8
- map.set(key, content);
9
- }
10
- return key;
11
- },
12
- get: function (key) {
13
- var val = map.get(key);
14
- return val;
15
- },
16
- };
17
- };
18
- var generateKey = function (value) {
19
- var hash = createHash('md5').update(value).digest('hex');
20
- return "#_#_".concat(hash, "_#_#");
21
- };
22
- export var contentMapKeyRE = /#_#_[0-9a-fA-F]{32}_#_#/g;
@@ -1,34 +0,0 @@
1
- import { createHash } from 'node:crypto'
2
-
3
- export type ContentMap = {
4
- /**
5
- * @returns key
6
- */
7
- add(title: string, sourceLength: number, content: string): string
8
- get(key: string): string | undefined
9
- }
10
-
11
- export const createContentMap = (): ContentMap => {
12
- const map = new Map<string, string>()
13
-
14
- return {
15
- add(title, length, content) {
16
- const key = generateKey(`${title}_${length}`)
17
- if (!map.has(key)) {
18
- map.set(key, content)
19
- }
20
- return key
21
- },
22
- get(key) {
23
- const val = map.get(key)
24
- return val
25
- },
26
- }
27
- }
28
-
29
- const generateKey = (value: string) => {
30
- const hash = createHash('md5').update(value).digest('hex')
31
- return `#_#_${hash}_#_#`
32
- }
33
-
34
- export const contentMapKeyRE = /#_#_[0-9a-fA-F]{32}_#_#/g