@readme/markdown 11.7.0 → 11.7.1

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.
@@ -0,0 +1,17 @@
1
+ interface BlockHit {
2
+ raw: string;
3
+ token: string;
4
+ }
5
+ /**
6
+ * Extract legacy magic block syntax from a markdown string.
7
+ * Returns the modified markdown and an array of extracted blocks.
8
+ */
9
+ export declare function extractMagicBlocks(markdown: string): {
10
+ replaced: string;
11
+ blocks: BlockHit[];
12
+ };
13
+ /**
14
+ * Restore extracted magic blocks back into a markdown string.
15
+ */
16
+ export declare function restoreMagicBlocks(replaced: string, blocks: BlockHit[]): string;
17
+ export {};
package/dist/main.js CHANGED
@@ -88109,23 +88109,62 @@ const stripCommentsTransformer = () => {
88109
88109
  };
88110
88110
  };
88111
88111
 
88112
+ ;// ./lib/utils/extractMagicBlocks.ts
88113
+ /**
88114
+ * The content matching in this regex captures everything between `[block:TYPE]`
88115
+ * and `[/block]`, including new lines. Negative lookahead for the closing
88116
+ * `[/block]` tag is required to prevent greedy matching to ensure it stops at
88117
+ * the first closing tag it encounters preventing vulnerability to polynomial
88118
+ * backtracking issues.
88119
+ */
88120
+ const MAGIC_BLOCK_REGEX = /\[block:[^\]]{1,100}\](?:(?!\[block:)(?!\[\/block\])[\s\S])*\[\/block\]/g;
88121
+ /**
88122
+ * Extract legacy magic block syntax from a markdown string.
88123
+ * Returns the modified markdown and an array of extracted blocks.
88124
+ */
88125
+ function extractMagicBlocks(markdown) {
88126
+ const blocks = [];
88127
+ let index = 0;
88128
+ const replaced = markdown.replace(MAGIC_BLOCK_REGEX, match => {
88129
+ // Use backticks so it becomes a code span, preventing remarkParse from
88130
+ // parsing special characters in the token as markdown syntax
88131
+ const token = `\`__MAGIC_BLOCK_${index}__\``;
88132
+ blocks.push({ token, raw: match });
88133
+ index += 1;
88134
+ return token;
88135
+ });
88136
+ return { replaced, blocks };
88137
+ }
88138
+ /**
88139
+ * Restore extracted magic blocks back into a markdown string.
88140
+ */
88141
+ function restoreMagicBlocks(replaced, blocks) {
88142
+ return blocks.reduce((acc, { token, raw }) => {
88143
+ return acc.split(token).join(raw);
88144
+ }, replaced);
88145
+ }
88146
+
88112
88147
  ;// ./lib/stripComments.ts
88113
88148
 
88114
88149
 
88115
88150
 
88116
88151
 
88117
88152
 
88153
+
88118
88154
  /**
88119
88155
  * Removes Markdown and MDX comments.
88120
88156
  */
88121
88157
  async function stripComments(doc, { mdx } = {}) {
88158
+ const { replaced, blocks } = extractMagicBlocks(doc);
88122
88159
  const processor = unified()
88123
88160
  .use(remarkParse)
88124
88161
  .use(mdx ? remarkMdx : undefined)
88125
88162
  .use(stripCommentsTransformer)
88126
88163
  .use(remarkStringify);
88127
- const file = await processor.process(doc);
88128
- return String(file);
88164
+ const file = await processor.process(replaced);
88165
+ const stringified = String(file).trim();
88166
+ const restored = restoreMagicBlocks(stringified, blocks);
88167
+ return restored;
88129
88168
  }
88130
88169
  /* harmony default export */ const lib_stripComments = (stripComments);
88131
88170
 
package/dist/main.node.js CHANGED
@@ -108320,23 +108320,62 @@ const stripCommentsTransformer = () => {
108320
108320
  };
108321
108321
  };
108322
108322
 
108323
+ ;// ./lib/utils/extractMagicBlocks.ts
108324
+ /**
108325
+ * The content matching in this regex captures everything between `[block:TYPE]`
108326
+ * and `[/block]`, including new lines. Negative lookahead for the closing
108327
+ * `[/block]` tag is required to prevent greedy matching to ensure it stops at
108328
+ * the first closing tag it encounters preventing vulnerability to polynomial
108329
+ * backtracking issues.
108330
+ */
108331
+ const MAGIC_BLOCK_REGEX = /\[block:[^\]]{1,100}\](?:(?!\[block:)(?!\[\/block\])[\s\S])*\[\/block\]/g;
108332
+ /**
108333
+ * Extract legacy magic block syntax from a markdown string.
108334
+ * Returns the modified markdown and an array of extracted blocks.
108335
+ */
108336
+ function extractMagicBlocks(markdown) {
108337
+ const blocks = [];
108338
+ let index = 0;
108339
+ const replaced = markdown.replace(MAGIC_BLOCK_REGEX, match => {
108340
+ // Use backticks so it becomes a code span, preventing remarkParse from
108341
+ // parsing special characters in the token as markdown syntax
108342
+ const token = `\`__MAGIC_BLOCK_${index}__\``;
108343
+ blocks.push({ token, raw: match });
108344
+ index += 1;
108345
+ return token;
108346
+ });
108347
+ return { replaced, blocks };
108348
+ }
108349
+ /**
108350
+ * Restore extracted magic blocks back into a markdown string.
108351
+ */
108352
+ function restoreMagicBlocks(replaced, blocks) {
108353
+ return blocks.reduce((acc, { token, raw }) => {
108354
+ return acc.split(token).join(raw);
108355
+ }, replaced);
108356
+ }
108357
+
108323
108358
  ;// ./lib/stripComments.ts
108324
108359
 
108325
108360
 
108326
108361
 
108327
108362
 
108328
108363
 
108364
+
108329
108365
  /**
108330
108366
  * Removes Markdown and MDX comments.
108331
108367
  */
108332
108368
  async function stripComments(doc, { mdx } = {}) {
108369
+ const { replaced, blocks } = extractMagicBlocks(doc);
108333
108370
  const processor = unified()
108334
108371
  .use(remarkParse)
108335
108372
  .use(mdx ? remarkMdx : undefined)
108336
108373
  .use(stripCommentsTransformer)
108337
108374
  .use(remarkStringify);
108338
- const file = await processor.process(doc);
108339
- return String(file);
108375
+ const file = await processor.process(replaced);
108376
+ const stringified = String(file).trim();
108377
+ const restored = restoreMagicBlocks(stringified, blocks);
108378
+ return restored;
108340
108379
  }
108341
108380
  /* harmony default export */ const lib_stripComments = (stripComments);
108342
108381