@brillout/docpress 0.15.10-commit-f89d08f → 0.15.10-commit-317b144
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 +20 -0
- package/dist/detypePlugin.js +20 -0
- package/package.json +1 -1
package/detypePlugin.ts
CHANGED
|
@@ -78,9 +78,17 @@ async function transformCode(code: string, moduleId: string) {
|
|
|
78
78
|
// Remove TypeScript
|
|
79
79
|
if (!isYaml) {
|
|
80
80
|
codeBlockContentJs = await detype(codeBlockContentJs, `some-dummy-filename.${codeBlockLang}`, {
|
|
81
|
+
customizeBabelConfig(config) {
|
|
82
|
+
// Add `onlyRemoveTypeImports: true` to the internal `@babel/preset-typescript` config
|
|
83
|
+
// See https://github.com/cyco130/detype/blob/main/src/transform.ts#L206
|
|
84
|
+
assertUsage(config.presets && config.presets.length === 1, 'Unexpected Babel config presets')
|
|
85
|
+
config.presets = [[config.presets[0], { onlyRemoveTypeImports: true }]]
|
|
86
|
+
},
|
|
81
87
|
removeTsComments: true,
|
|
82
88
|
prettierOptions,
|
|
83
89
|
})
|
|
90
|
+
// Correct code diff comments
|
|
91
|
+
codeBlockContentJs = correctCodeDiffComments(codeBlockContentJs)
|
|
84
92
|
}
|
|
85
93
|
|
|
86
94
|
// Update code block open delimiter
|
|
@@ -157,3 +165,15 @@ function processMagicComments(code: string) {
|
|
|
157
165
|
|
|
158
166
|
return code.replaceAll('//~', '')
|
|
159
167
|
}
|
|
168
|
+
/**
|
|
169
|
+
* Correct code diff comments that detype() unexpectedly reformatted (using Prettier and Babel internally)
|
|
170
|
+
* after removing TypeScript.
|
|
171
|
+
* See https://github.com/brillout/docpress/pull/47#issuecomment-3263953899
|
|
172
|
+
* @param code Transformed Javascript code.
|
|
173
|
+
* @returns The corrected code.
|
|
174
|
+
*/
|
|
175
|
+
function correctCodeDiffComments(code: string) {
|
|
176
|
+
// Expected to match the code diff comments: `// [!code ++]` and `// [!code --]` started with newline and optional spaces
|
|
177
|
+
const codeDiffRE = /\n\s*\/\/\s\[!code.+\]/g
|
|
178
|
+
return code.replaceAll(codeDiffRE, (codeDiff) => codeDiff.trimStart())
|
|
179
|
+
}
|
package/dist/detypePlugin.js
CHANGED
|
@@ -70,9 +70,17 @@ async function transformCode(code, moduleId) {
|
|
|
70
70
|
// Remove TypeScript
|
|
71
71
|
if (!isYaml) {
|
|
72
72
|
codeBlockContentJs = await detype(codeBlockContentJs, `some-dummy-filename.${codeBlockLang}`, {
|
|
73
|
+
customizeBabelConfig(config) {
|
|
74
|
+
// Add `onlyRemoveTypeImports: true` to the internal `@babel/preset-typescript` config
|
|
75
|
+
// See https://github.com/cyco130/detype/blob/main/src/transform.ts#L206
|
|
76
|
+
assertUsage(config.presets && config.presets.length === 1, 'Unexpected Babel config presets');
|
|
77
|
+
config.presets = [[config.presets[0], { onlyRemoveTypeImports: true }]];
|
|
78
|
+
},
|
|
73
79
|
removeTsComments: true,
|
|
74
80
|
prettierOptions,
|
|
75
81
|
});
|
|
82
|
+
// Correct code diff comments
|
|
83
|
+
codeBlockContentJs = correctCodeDiffComments(codeBlockContentJs);
|
|
76
84
|
}
|
|
77
85
|
// Update code block open delimiter
|
|
78
86
|
const codeBlockLangJs = codeBlockLang === 'vue'
|
|
@@ -137,3 +145,15 @@ function processMagicComments(code) {
|
|
|
137
145
|
}
|
|
138
146
|
return code.replaceAll('//~', '');
|
|
139
147
|
}
|
|
148
|
+
/**
|
|
149
|
+
* Correct code diff comments that detype() unexpectedly reformatted (using Prettier and Babel internally)
|
|
150
|
+
* after removing TypeScript.
|
|
151
|
+
* See https://github.com/brillout/docpress/pull/47#issuecomment-3263953899
|
|
152
|
+
* @param code Transformed Javascript code.
|
|
153
|
+
* @returns The corrected code.
|
|
154
|
+
*/
|
|
155
|
+
function correctCodeDiffComments(code) {
|
|
156
|
+
// Expected to match the code diff comments: `// [!code ++]` and `// [!code --]` started with newline and optional spaces
|
|
157
|
+
const codeDiffRE = /\n\s*\/\/\s\[!code.+\]/g;
|
|
158
|
+
return code.replaceAll(codeDiffRE, (codeDiff) => codeDiff.trimStart());
|
|
159
|
+
}
|