Skip to content

Commit 7503338

Browse files
committed
feat(block-transformer): add forceRemoveComments option
Bypasses safety check and strips comments directly in updatedContents, useful for multi-pass scenarios where input is already processed output.
1 parent cbaa34e commit 7503338

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

packages/block-transformer/src/index.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ const CLOSE_WORD = '/block'
6666
* @property {Array<Middleware>} [beforeMiddleware=[]] - Middleware functions change inner block content before transforms.
6767
* @property {Array<Middleware>} [afterMiddleware=[]] - Middleware functions change inner block content after transforms.
6868
* @property {boolean} [removeComments=false] - Remove comments from the processed contents.
69+
* @property {boolean} [forceRemoveComments=false] - Force remove comments even when srcPath === outputPath, strips comments directly in updatedContents.
6970
* @property {string} [srcPath] - The source path.
7071
* @property {string} [outputPath] - The output path.
7172
* @property {import('comment-block-parser').CustomPatterns} [customPatterns] - Custom regex patterns for open and close tags.
@@ -103,6 +104,7 @@ async function blockTransformer(inputText, config) {
103104
beforeMiddleware = [],
104105
afterMiddleware = [],
105106
removeComments = false,
107+
forceRemoveComments = false,
106108
customPatterns
107109
} = opts
108110
// Don't default close - let undefined pass through to enable pattern mode in block-parser
@@ -285,15 +287,19 @@ async function blockTransformer(inputText, config) {
285287

286288
const isNewPath = srcPath !== outputPath
287289

288-
if (removeComments && !isNewPath) {
290+
if (removeComments && !isNewPath && !forceRemoveComments) {
289291
throw new Error('"removeComments" can only be used if "outputPath" option is set. Otherwise this will break doc generation.')
290292
}
291293

292-
const stripComments = isNewPath && removeComments
294+
const stripComments = (isNewPath && removeComments) || forceRemoveComments
293295

296+
let finalContents = updatedContents
297+
if (forceRemoveComments && openPattern && closePattern) {
298+
finalContents = updatedContents.replace(openPattern, '').replace(closePattern, '')
299+
}
294300

295301
// console.log('inputText', inputText)
296-
// console.log('updatedContents', updatedContents)
302+
// console.log('updatedContents', updatedContents)
297303
return {
298304
isChanged: inputText !== updatedContents,
299305
isNewPath,
@@ -303,7 +309,7 @@ async function blockTransformer(inputText, config) {
303309
transforms: transformsToRun,
304310
missingTransforms,
305311
originalContents: inputText,
306-
updatedContents,
312+
updatedContents: finalContents,
307313
patterns: regexInfo,
308314
}
309315
}

packages/block-transformer/test/error-handling.test.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,4 +248,44 @@ original content
248248
assert.ok(result.updatedContents.includes('original content'))
249249
})
250250

251+
test('forceRemoveComments bypasses safety check and strips comments from updatedContents', async () => {
252+
const text = `
253+
<!-- block test -->
254+
content
255+
<!-- /block -->
256+
`
257+
const result = await blockTransformer(text, {
258+
srcPath: '/same/path.md',
259+
outputPath: '/same/path.md',
260+
forceRemoveComments: true,
261+
transforms: {
262+
test: (api) => 'transformed'
263+
}
264+
})
265+
266+
assert.is(result.stripComments, true)
267+
assert.not.ok(result.updatedContents.includes('<!-- block'))
268+
assert.not.ok(result.updatedContents.includes('<!-- /block'))
269+
assert.ok(result.updatedContents.includes('transformed'))
270+
})
271+
272+
test('forceRemoveComments works without outputPath', async () => {
273+
const text = `
274+
<!-- block test -->
275+
content
276+
<!-- /block -->
277+
`
278+
const result = await blockTransformer(text, {
279+
forceRemoveComments: true,
280+
transforms: {
281+
test: (api) => 'transformed'
282+
}
283+
})
284+
285+
assert.is(result.stripComments, true)
286+
assert.not.ok(result.updatedContents.includes('<!-- block'))
287+
assert.not.ok(result.updatedContents.includes('<!-- /block'))
288+
assert.ok(result.updatedContents.includes('transformed'))
289+
})
290+
251291
test.run()

0 commit comments

Comments
 (0)