@asciidoctor/core 4.0.1 → 4.0.3

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/src/extensions.js CHANGED
@@ -54,6 +54,30 @@ import { MacroNameRx, CC_ANY } from './rx.js'
54
54
  * @typedef {ProcessorDslInterface & { prefer(): void; prepend(): void }} DocumentProcessorDslInterface
55
55
  */
56
56
 
57
+ /**
58
+ * DSL interface for preprocessors.
59
+ *
60
+ * @typedef {Omit<DocumentProcessorDslInterface, 'process'> & {
61
+ * process(fn: (this: PreprocessorDslInterface, document: Document, reader: Reader) => Reader | void): void;
62
+ * }} PreprocessorDslInterface
63
+ */
64
+
65
+ /**
66
+ * DSL interface for tree processors.
67
+ *
68
+ * @typedef {Omit<DocumentProcessorDslInterface, 'process'> & {
69
+ * process(fn: (this: TreeProcessorDslInterface, document: Document) => Document | void): void;
70
+ * }} TreeProcessorDslInterface
71
+ */
72
+
73
+ /**
74
+ * DSL interface for postprocessors.
75
+ *
76
+ * @typedef {Omit<DocumentProcessorDslInterface, 'process'> & {
77
+ * process(fn: (this: PostprocessorDslInterface, document: Document, output: string) => string): void;
78
+ * }} PostprocessorDslInterface
79
+ */
80
+
57
81
  /**
58
82
  * DSL interface for syntax processors (BlockProcessor, BlockMacroProcessor, InlineMacroProcessor).
59
83
  *
@@ -74,28 +98,35 @@ import { MacroNameRx, CC_ANY } from './rx.js'
74
98
  /**
75
99
  * DSL interface for include processors.
76
100
  *
77
- * @typedef {DocumentProcessorDslInterface & {
101
+ * @typedef {Omit<DocumentProcessorDslInterface, 'process'> & {
78
102
  * handles(fn: (target: string) => boolean): void;
79
103
  * handles(fn: (doc: Document, target: string) => boolean): void;
104
+ * process(fn: (this: IncludeProcessorDslInterface, document: Document, reader: Reader, target: string, attributes: Record<string, string>) => void): void;
80
105
  * }} IncludeProcessorDslInterface
81
106
  */
82
107
 
83
108
  /**
84
109
  * DSL interface for docinfo processors.
85
110
  *
86
- * @typedef {DocumentProcessorDslInterface & {
111
+ * @typedef {Omit<DocumentProcessorDslInterface, 'process'> & {
87
112
  * atLocation(value: string): void;
113
+ * process(fn: (this: DocinfoProcessorDslInterface, document: Document) => string): void;
88
114
  * }} DocinfoProcessorDslInterface
89
115
  */
90
116
 
91
117
  /**
92
118
  * DSL interface for block processors.
93
119
  *
94
- * @typedef {SyntaxProcessorDslInterface & {
120
+ * The `process` callback is bound to the processor instance, so `this` inside it
121
+ * (and inside the registration function) exposes the `createBlock` helpers.
122
+ *
123
+ * @typedef {Omit<SyntaxProcessorDslInterface, 'process'> & {
95
124
  * contexts(...value: (string | string[])[]): void;
96
125
  * onContexts(...value: (string | string[])[]): void;
97
126
  * onContext(...value: (string | string[])[]): void;
98
127
  * bindTo(...value: (string | string[])[]): void;
128
+ * createBlock(parent: AbstractBlock, context: string, source?: string | string[] | null, attrs?: object, opts?: object): Block;
129
+ * process(fn: (this: BlockProcessorDslInterface, parent: AbstractBlock, reader: Reader, attributes: Record<string, unknown>) => AbstractBlock | void): void;
99
130
  * }} BlockProcessorDslInterface
100
131
  */
101
132
 
@@ -105,14 +136,31 @@ import { MacroNameRx, CC_ANY } from './rx.js'
105
136
  * @typedef {SyntaxProcessorDslInterface} MacroProcessorDslInterface
106
137
  */
107
138
 
139
+ /**
140
+ * DSL interface for block macro processors.
141
+ *
142
+ * The `process` callback is bound to the processor instance, so `this` inside it
143
+ * (and inside the registration function) exposes the `createBlock` helpers.
144
+ *
145
+ * @typedef {Omit<MacroProcessorDslInterface, 'process'> & {
146
+ * createBlock(parent: AbstractBlock, context: string, source?: string | string[] | null, attrs?: object, opts?: object): Block;
147
+ * process(fn: (this: BlockMacroProcessorDslInterface, parent: AbstractBlock, target: string, attributes: Record<string, unknown>) => AbstractBlock | void): void;
148
+ * }} BlockMacroProcessorDslInterface
149
+ */
150
+
108
151
  /**
109
152
  * DSL interface for inline macro processors.
110
153
  *
111
- * @typedef {MacroProcessorDslInterface & {
154
+ * The `process` callback is bound to the processor instance, so `this` inside it
155
+ * (and inside the registration function) exposes the `createInline` helper.
156
+ *
157
+ * @typedef {Omit<MacroProcessorDslInterface, 'process'> & {
112
158
  * format(value: string): void;
113
159
  * matchFormat(value: string): void;
114
160
  * usingFormat(value: string): void;
115
161
  * match(value: RegExp): void;
162
+ * createInline(parent: AbstractBlock, context: string, text: string, opts?: object): Inline;
163
+ * process(fn: (this: InlineMacroProcessorDslInterface, parent: AbstractBlock, target: string, attributes: Record<string, unknown>) => Inline | void): void;
116
164
  * }} InlineMacroProcessorDslInterface
117
165
  */
118
166
 
@@ -839,7 +887,15 @@ export class IncludeProcessor extends Processor {
839
887
  }
840
888
 
841
889
  /**
842
- * @param {Document} doc - The document being parsed.
890
+ * Decide whether this include processor handles the given target.
891
+ *
892
+ * Override this method in a subclass. The override may accept either just the
893
+ * target string (Ruby-style, arity 1) or both the document and the target
894
+ * (arity 2) — an arity-1 override is adapted at registration time so the
895
+ * parser can always invoke it as `handles(doc, target)`. The first parameter
896
+ * is therefore typed `Document | string` so both override shapes type-check.
897
+ *
898
+ * @param {Document|string} doc - The document being parsed, or (for a Ruby-style arity-1 override) the include target.
843
899
  * @param {string} target - The target of the include directive.
844
900
  * @returns {boolean} true if this processor handles the given target.
845
901
  */
@@ -1251,6 +1307,14 @@ export class Registry {
1251
1307
  * this.process(function (doc, reader) { ... })
1252
1308
  * })
1253
1309
  *
1310
+ * @overload
1311
+ * @param {typeof Preprocessor} processor - A Preprocessor subclass or instance.
1312
+ * @returns {ProcessorExtension} the Extension stored in the registry.
1313
+ *
1314
+ * @overload
1315
+ * @param {(this: PreprocessorDslInterface) => void} fn - Registration function bound to the preprocessor DSL.
1316
+ * @returns {ProcessorExtension} the Extension stored in the registry.
1317
+ *
1254
1318
  * @param {...*} args - Class constructor, instance, or block function.
1255
1319
  * @returns {ProcessorExtension} the Extension stored in the registry.
1256
1320
  */
@@ -1284,6 +1348,14 @@ export class Registry {
1284
1348
  /**
1285
1349
  * Register a TreeProcessor with the extension registry.
1286
1350
  *
1351
+ * @overload
1352
+ * @param {typeof TreeProcessor} processor - A TreeProcessor subclass or instance.
1353
+ * @returns {ProcessorExtension} the Extension stored in the registry.
1354
+ *
1355
+ * @overload
1356
+ * @param {(this: TreeProcessorDslInterface) => void} fn - Registration function bound to the tree processor DSL.
1357
+ * @returns {ProcessorExtension} the Extension stored in the registry.
1358
+ *
1287
1359
  * @param {...*} args - Class constructor, instance, or block function.
1288
1360
  * @returns {ProcessorExtension} the Extension stored in the registry.
1289
1361
  */
@@ -1337,6 +1409,14 @@ export class Registry {
1337
1409
  /**
1338
1410
  * Register a Postprocessor with the extension registry.
1339
1411
  *
1412
+ * @overload
1413
+ * @param {typeof Postprocessor} processor - A Postprocessor subclass or instance.
1414
+ * @returns {ProcessorExtension} the Extension stored in the registry.
1415
+ *
1416
+ * @overload
1417
+ * @param {(this: PostprocessorDslInterface) => void} fn - Registration function bound to the postprocessor DSL.
1418
+ * @returns {ProcessorExtension} the Extension stored in the registry.
1419
+ *
1340
1420
  * @param {...*} args - Class constructor, instance, or block function.
1341
1421
  * @returns {ProcessorExtension} the Extension stored in the registry.
1342
1422
  */
@@ -1370,6 +1450,14 @@ export class Registry {
1370
1450
  /**
1371
1451
  * Register an IncludeProcessor with the extension registry.
1372
1452
  *
1453
+ * @overload
1454
+ * @param {typeof IncludeProcessor} processor - An IncludeProcessor subclass or instance.
1455
+ * @returns {ProcessorExtension} the Extension stored in the registry.
1456
+ *
1457
+ * @overload
1458
+ * @param {(this: IncludeProcessorDslInterface) => void} fn - Registration function bound to the include processor DSL.
1459
+ * @returns {ProcessorExtension} the Extension stored in the registry.
1460
+ *
1373
1461
  * @param {...*} args - Class constructor, instance, or block function.
1374
1462
  * @returns {ProcessorExtension} the Extension stored in the registry.
1375
1463
  */
@@ -1408,6 +1496,14 @@ export class Registry {
1408
1496
  /**
1409
1497
  * Register a DocinfoProcessor with the extension registry.
1410
1498
  *
1499
+ * @overload
1500
+ * @param {typeof DocinfoProcessor} processor - A DocinfoProcessor subclass or instance.
1501
+ * @returns {ProcessorExtension} the Extension stored in the registry.
1502
+ *
1503
+ * @overload
1504
+ * @param {(this: DocinfoProcessorDslInterface) => void} fn - Registration function bound to the docinfo processor DSL.
1505
+ * @returns {ProcessorExtension} the Extension stored in the registry.
1506
+ *
1411
1507
  * @param {...*} args - Class constructor, instance, or block function.
1412
1508
  * @returns {ProcessorExtension} the Extension stored in the registry.
1413
1509
  */
@@ -1475,6 +1571,20 @@ export class Registry {
1475
1571
  * this.process(function (parent, reader, attrs) { ... })
1476
1572
  * })
1477
1573
  *
1574
+ * @overload
1575
+ * @param {typeof BlockProcessor} processor - A BlockProcessor subclass.
1576
+ * @param {string} [name] - Optional explicit block name.
1577
+ * @returns {ProcessorExtension} an Extension proxy object.
1578
+ *
1579
+ * @overload
1580
+ * @param {string} name - The block name.
1581
+ * @param {(this: BlockProcessorDslInterface) => void} fn - Registration function bound to the block DSL.
1582
+ * @returns {ProcessorExtension} an Extension proxy object.
1583
+ *
1584
+ * @overload
1585
+ * @param {(this: BlockProcessorDslInterface) => void} fn - Registration function bound to the block DSL.
1586
+ * @returns {ProcessorExtension} an Extension proxy object.
1587
+ *
1478
1588
  * @param {...*} args - Class constructor, instance, block function, or name + one of those.
1479
1589
  * @returns {ProcessorExtension} an Extension proxy object.
1480
1590
  */
@@ -1525,6 +1635,20 @@ export class Registry {
1525
1635
  /**
1526
1636
  * Register a BlockMacroProcessor with the extension registry.
1527
1637
  *
1638
+ * @overload
1639
+ * @param {typeof BlockMacroProcessor} processor - A BlockMacroProcessor subclass.
1640
+ * @param {string} [name] - Optional explicit macro name.
1641
+ * @returns {ProcessorExtension} the Extension stored in the registry.
1642
+ *
1643
+ * @overload
1644
+ * @param {string} name - The macro name.
1645
+ * @param {(this: BlockMacroProcessorDslInterface) => void} fn - Registration function bound to the block macro DSL.
1646
+ * @returns {ProcessorExtension} the Extension stored in the registry.
1647
+ *
1648
+ * @overload
1649
+ * @param {(this: BlockMacroProcessorDslInterface) => void} fn - Registration function bound to the block macro DSL.
1650
+ * @returns {ProcessorExtension} the Extension stored in the registry.
1651
+ *
1528
1652
  * @param {...*} args - Class constructor, instance, or block function.
1529
1653
  * @returns {ProcessorExtension} the Extension stored in the registry.
1530
1654
  */
@@ -1580,6 +1704,20 @@ export class Registry {
1580
1704
  /**
1581
1705
  * Register an InlineMacroProcessor with the extension registry.
1582
1706
  *
1707
+ * @overload
1708
+ * @param {typeof InlineMacroProcessor} processor - An InlineMacroProcessor subclass.
1709
+ * @param {string} [name] - Optional explicit macro name.
1710
+ * @returns {ProcessorExtension} the Extension stored in the registry.
1711
+ *
1712
+ * @overload
1713
+ * @param {string} name - The macro name.
1714
+ * @param {(this: InlineMacroProcessorDslInterface) => void} fn - Registration function bound to the inline macro DSL.
1715
+ * @returns {ProcessorExtension} the Extension stored in the registry.
1716
+ *
1717
+ * @overload
1718
+ * @param {(this: InlineMacroProcessorDslInterface) => void} fn - Registration function bound to the inline macro DSL.
1719
+ * @returns {ProcessorExtension} the Extension stored in the registry.
1720
+ *
1583
1721
  * @param {...*} args - Class constructor, instance, or block function.
1584
1722
  * @returns {ProcessorExtension} the Extension stored in the registry.
1585
1723
  */
package/src/index.js CHANGED
@@ -40,11 +40,15 @@ import {
40
40
  /**
41
41
  * @typedef {import('./extensions.js').ProcessorDslInterface} ProcessorDslInterface
42
42
  * @typedef {import('./extensions.js').DocumentProcessorDslInterface} DocumentProcessorDslInterface
43
+ * @typedef {import('./extensions.js').PreprocessorDslInterface} PreprocessorDslInterface
44
+ * @typedef {import('./extensions.js').TreeProcessorDslInterface} TreeProcessorDslInterface
45
+ * @typedef {import('./extensions.js').PostprocessorDslInterface} PostprocessorDslInterface
43
46
  * @typedef {import('./extensions.js').SyntaxProcessorDslInterface} SyntaxProcessorDslInterface
44
47
  * @typedef {import('./extensions.js').IncludeProcessorDslInterface} IncludeProcessorDslInterface
45
48
  * @typedef {import('./extensions.js').DocinfoProcessorDslInterface} DocinfoProcessorDslInterface
46
49
  * @typedef {import('./extensions.js').BlockProcessorDslInterface} BlockProcessorDslInterface
47
50
  * @typedef {import('./extensions.js').MacroProcessorDslInterface} MacroProcessorDslInterface
51
+ * @typedef {import('./extensions.js').BlockMacroProcessorDslInterface} BlockMacroProcessorDslInterface
48
52
  * @typedef {import('./extensions.js').InlineMacroProcessorDslInterface} InlineMacroProcessorDslInterface
49
53
  */
50
54
  import {
@@ -234,9 +234,10 @@ export abstract class AbstractNode {
234
234
  * Construct a URI reference or data URI to the target image.
235
235
  *
236
236
  * If the target image is already a URI it is left untouched (unless data-uri
237
- * conversion is requested). The image is resolved relative to the directory
238
- * named by assetDirKey. When data-uri is enabled and the safe level permits,
239
- * the image is embedded as a Base64 data URI.
237
+ * conversion is requested). If the target image is a data URI, then it is
238
+ * already an embedded image, so it is returned as-is. The image is resolved
239
+ * relative to the directory named by assetDirKey. When data-uri is enabled and
240
+ * the safe level permits, the image is embedded as a Base64 data URI.
240
241
  *
241
242
  * NOTE: When the document has both 'data-uri' and 'allow-uri-read' enabled
242
243
  * and the resolved image URL is a remote URI, this method returns a Promise
@@ -469,10 +469,26 @@ export class Document extends AbstractBlock<string> {
469
469
  * @returns {string[]} The list of substitutions to apply.
470
470
  */
471
471
  private _resolveDocinfoSubs;
472
+ /**
473
+ * @private
474
+ * Restore the document attributes to a previously captured snapshot, discarding any
475
+ * body-level (re)assignments replayed while pre-computing text. Mirrors Ruby's
476
+ * restore_attributes-before-convert invariant.
477
+ * @param {Object} snapshot - The attributes snapshot to restore.
478
+ */
479
+ private _restoreAttributeSnapshot;
472
480
  /**
473
481
  * @private
474
482
  * Walk the block tree and pre-compute all async text values.
475
483
  * Handles titles (AbstractBlock), list item text, table cell text, and reftexts.
484
+ *
485
+ * Runs in two passes (see {@link parse}): with `resolveContent` false only titles and
486
+ * reftexts are substituted (so the reftext→id map can be built); with `resolveContent`
487
+ * true the list item / table cell / dlist text is substituted, resolving any natural
488
+ * cross-references against the now-complete map. Title/reftext pre-computation is
489
+ * idempotent (results are cached), so running it in both passes is a no-op the second time.
490
+ * @param {AbstractBlock} block - The block to resolve.
491
+ * @param {boolean} resolveContent - Whether to substitute list item / cell / dlist text.
476
492
  */
477
493
  private _resolveAllTexts;
478
494
  /**