@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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@asciidoctor/core",
3
- "version": "4.0.1",
3
+ "version": "4.0.3",
4
4
  "description": "Asciidoctor.js: AsciiDoc in JavaScript powered by Asciidoctor",
5
5
  "type": "module",
6
6
  "main": "build/node/index.cjs",
@@ -31,6 +31,7 @@
31
31
  "test:deno": "deno test --allow-env --no-check --allow-read --allow-sys --allow-write --allow-run --allow-net test/*.test.js",
32
32
  "test:coverage": "node --test --experimental-test-coverage --test-coverage-include='src/**' test/**.test.js",
33
33
  "test:browser": "vitest run --config vitest.browser.config.js",
34
+ "test:types": "tsc -p test/types/tsconfig.json",
34
35
  "lint": "biome lint src test",
35
36
  "format": "biome format --write src test",
36
37
  "check": "biome check src test"
@@ -461,9 +461,10 @@ export class AbstractNode {
461
461
  * Construct a URI reference or data URI to the target image.
462
462
  *
463
463
  * If the target image is already a URI it is left untouched (unless data-uri
464
- * conversion is requested). The image is resolved relative to the directory
465
- * named by assetDirKey. When data-uri is enabled and the safe level permits,
466
- * the image is embedded as a Base64 data URI.
464
+ * conversion is requested). If the target image is a data URI, then it is
465
+ * already an embedded image, so it is returned as-is. The image is resolved
466
+ * relative to the directory named by assetDirKey. When data-uri is enabled and
467
+ * the safe level permits, the image is embedded as a Base64 data URI.
467
468
  *
468
469
  * NOTE: When the document has both 'data-uri' and 'allow-uri-read' enabled
469
470
  * and the resolved image URL is a remote URI, this method returns a Promise
@@ -474,6 +475,8 @@ export class AbstractNode {
474
475
  * @returns {Promise<string>} a Promise resolving to a String reference or data URI.
475
476
  */
476
477
  async imageUri(targetImage, assetDirKey = 'imagesdir') {
478
+ // A data URI is already an embedded image, so use it as-is rather than reading or re-encoding it.
479
+ if (targetImage.startsWith('data:')) return targetImage
477
480
  const doc = this.document
478
481
  if (doc.safe < SafeMode.SECURE && doc.hasAttribute('data-uri')) {
479
482
  let imagesBase
@@ -116,7 +116,7 @@ export default class Html5Converter extends ConverterBase {
116
116
  let reproducible
117
117
  if (!(reproducible = node.hasAttribute('reproducible'))) {
118
118
  result.push(
119
- `<meta name="generator" content="Asciidoctor ${node.getAttribute('asciidoctor-version')}"${slash}>`
119
+ `<meta name="generator" content="Asciidoctor.js ${node.getAttribute('asciidoctor-version')}"${slash}>`
120
120
  )
121
121
  }
122
122
  if (node.hasAttribute('app-name')) {
@@ -833,7 +833,9 @@ ${await node.content()}
833
833
  const slash = this._voidSlash
834
834
  let img, src
835
835
  if (
836
- (node.hasAttribute('format', 'svg') || target.includes('.svg')) &&
836
+ (node.hasAttribute('format', 'svg') ||
837
+ target.includes('.svg') ||
838
+ target.startsWith('data:image/svg+xml')) &&
837
839
  node.document.safe < SafeMode.SECURE
838
840
  ) {
839
841
  if (node.hasOption('inline')) {
@@ -1674,7 +1676,9 @@ Your browser does not support the video tag.
1674
1676
  if (node.hasAttribute('title'))
1675
1677
  attrs += ` title="${node.getAttribute('title')}"`
1676
1678
  if (
1677
- (node.hasAttribute('format', 'svg') || target.includes('.svg')) &&
1679
+ (node.hasAttribute('format', 'svg') ||
1680
+ target.includes('.svg') ||
1681
+ target.startsWith('data:image/svg+xml')) &&
1678
1682
  node.document.safe < SafeMode.SECURE
1679
1683
  ) {
1680
1684
  if (node.hasOption('inline')) {
@@ -1767,12 +1771,17 @@ Your browser does not support the video tag.
1767
1771
 
1768
1772
  // NOTE expose readSvgContents for Bespoke converter
1769
1773
  async readSvgContents(node, target) {
1770
- let svg = await node.readContents(target, {
1771
- start: node.document.getAttribute('imagesdir'),
1772
- normalize: true,
1773
- label: 'SVG',
1774
- warnIfEmpty: true,
1775
- })
1774
+ // A data-URI carries the SVG in the target itself (e.g. an embedded diagram
1775
+ // produced with `:data-uri:`), so decode it directly rather than trying to
1776
+ // read it as a file or remote URI.
1777
+ let svg = target.startsWith('data:')
1778
+ ? this._decodeDataUri(target)
1779
+ : await node.readContents(target, {
1780
+ start: node.document.getAttribute('imagesdir'),
1781
+ normalize: true,
1782
+ label: 'SVG',
1783
+ warnIfEmpty: true,
1784
+ })
1776
1785
  if (!svg) return null
1777
1786
  if (!svg.startsWith('<svg')) svg = svg.replace(SvgPreambleRx, '')
1778
1787
  // Fix incomplete SVG start tag (missing closing >) by inserting > before the first child element.
@@ -1804,6 +1813,27 @@ Your browser does not support the video tag.
1804
1813
 
1805
1814
  // ── Private helpers ─────────────────────────────────────────────────────────
1806
1815
 
1816
+ /**
1817
+ * Decode an inline `data:` URI to its text contents (e.g. an SVG document) so
1818
+ * an image whose target is a data-URI can be embedded inline. Supports both
1819
+ * Base64 (`;base64,`) and percent-encoded payloads. Returns null when the
1820
+ * payload is missing.
1821
+ *
1822
+ * @internal
1823
+ * @private
1824
+ */
1825
+ _decodeDataUri(target) {
1826
+ const comma = target.indexOf(',')
1827
+ if (comma === -1) return null
1828
+ const meta = target.slice('data:'.length, comma)
1829
+ const data = target.slice(comma + 1)
1830
+ if (/;base64\b/i.test(meta)) {
1831
+ const bytes = Uint8Array.from(atob(data), (c) => c.charCodeAt(0))
1832
+ return new TextDecoder('utf-8').decode(bytes)
1833
+ }
1834
+ return decodeURIComponent(data)
1835
+ }
1836
+
1807
1837
  /**
1808
1838
  * @internal
1809
1839
  * @private
@@ -108,7 +108,7 @@ export default class ManPageConverter extends ConverterBase {
108
108
  `'\\" t
109
109
  .\\" Title: ${mantitle}
110
110
  .\\" Author: ${node.hasAttribute('authors') ? node.getAttribute('authors') : '[see the "AUTHOR(S)" section]'}
111
- .\\" Generator: Asciidoctor ${node.getAttribute('asciidoctor-version')}`,
111
+ .\\" Generator: Asciidoctor.js ${node.getAttribute('asciidoctor-version')}`,
112
112
  ]
113
113
 
114
114
  if (docdate) result.push(`.\\" Date: ${docdate}`)
@@ -8,8 +8,7 @@
8
8
  // - Ruby File.directory?/File.file? → fsp.stat().isDirectory()/isFile() (async).
9
9
  // - Ruby File.basename / File.expand_path → node:path basename / resolve.
10
10
  // - PathResolver.system_path → pathResolver.systemPath().
11
- // - template.render(node, opts) → template.render({node, opts, helpers}).
12
- // - Helpers loaded from helpers.js/helpers.cjs; can export configure(enginesContext).
11
+ // - template.render(node, opts) → template.render({node, opts, helpers}).// - Helpers loaded from helpers.js/helpers.cjs/helpers.mjs; can export configure(enginesContext).
13
12
  // - Custom engines registered via TemplateConverter.TemplateEngine.register(ext, adapter).
14
13
  // - Thread safety / Mutex → not needed (single-threaded JS).
15
14
  // - load_eruby / eRuby support → not applicable in JS environment.
@@ -19,6 +18,7 @@
19
18
  import { ConverterBase } from '../converter.js'
20
19
  import { PathResolver } from '../path_resolver.js'
21
20
  import { createRequire } from 'node:module'
21
+ import { pathToFileURL } from 'node:url'
22
22
  import { promises as fsp } from 'node:fs'
23
23
  import path from 'node:path'
24
24
 
@@ -288,7 +288,11 @@ export class TemplateConverter extends ConverterBase {
288
288
  if (!(await _isFile(file))) continue
289
289
 
290
290
  // Collect helpers separately; process after all templates.
291
- if (basename === 'helpers.js' || basename === 'helpers.cjs') {
291
+ if (
292
+ basename === 'helpers.js' ||
293
+ basename === 'helpers.cjs' ||
294
+ basename === 'helpers.mjs'
295
+ ) {
292
296
  helpersFile = file
293
297
  continue
294
298
  }
@@ -356,8 +360,8 @@ export class TemplateConverter extends ConverterBase {
356
360
  const pugOpts = { ...(this.engineOptions.pug ?? {}), filename: file }
357
361
  const renderFn = pug.compileFile(file, pugOpts)
358
362
  template = { render: renderFn, file }
359
- } else if (ext === 'js' || ext === 'cjs') {
360
- const renderFn = _require(file)
363
+ } else if (ext === 'js' || ext === 'cjs' || ext === 'mjs') {
364
+ const renderFn = await _loadModule(file, ext)
361
365
  template = { render: renderFn, file }
362
366
  } else {
363
367
  // Fall back to custom TemplateEngine registry.
@@ -372,16 +376,20 @@ export class TemplateConverter extends ConverterBase {
372
376
  result[name] = template
373
377
  }
374
378
 
375
- // Load helpers if found (or if a helpers.js exists at the top of the dir).
376
- const fallbackHelpers = path.join(templateDir, 'helpers.js')
377
- const fallbackHelpersCjs = path.join(templateDir, 'helpers.cjs')
378
- if (!helpersFile && (await _isFile(fallbackHelpers)))
379
- helpersFile = fallbackHelpers
380
- if (!helpersFile && (await _isFile(fallbackHelpersCjs)))
381
- helpersFile = fallbackHelpersCjs
379
+ // Load helpers if found (or if a helpers file exists at the top of the dir).
380
+ if (!helpersFile) {
381
+ for (const ext of ['js', 'cjs', 'mjs']) {
382
+ const fallback = path.join(templateDir, `helpers.${ext}`)
383
+ if (await _isFile(fallback)) {
384
+ helpersFile = fallback
385
+ break
386
+ }
387
+ }
388
+ }
382
389
 
383
390
  if (helpersFile) {
384
- const ctx = _require(helpersFile)
391
+ const helpersExt = helpersFile.slice(helpersFile.lastIndexOf('.') + 1)
392
+ const ctx = await _loadModule(helpersFile, helpersExt)
385
393
  if (typeof ctx.configure === 'function') ctx.configure(enginesCtx)
386
394
  result['helpers.js'] = { file: helpersFile, ctx }
387
395
  }
@@ -409,6 +417,32 @@ export class TemplateConverter extends ConverterBase {
409
417
 
410
418
  // ── Helpers ───────────────────────────────────────────────────────────────────
411
419
 
420
+ /**
421
+ * Load a JavaScript module (template render function or helpers object) by
422
+ * file extension.
423
+ *
424
+ * - `.cjs` files are always CommonJS: require() returns `module.exports`.
425
+ * - `.js` and `.mjs` files are loaded with a dynamic `import()`, which Node
426
+ * resolves as either ES modules or CommonJS. The export is taken from the
427
+ * module's default export (`export default …` in ESM, `module.exports = …`
428
+ * in CommonJS via interop); when there is no default export the module
429
+ * namespace is returned instead, so ESM helpers exposing named exports
430
+ * (`export function configure() {}`) work too.
431
+ * @param {string} file - absolute path to the module file
432
+ * @param {string} ext - file extension without the leading dot ('js' | 'cjs' | 'mjs')
433
+ * @returns {Promise<*>} the module's default export, or its namespace
434
+ * @internal
435
+ * @private
436
+ */
437
+ async function _loadModule(file, ext) {
438
+ if (ext === 'cjs') {
439
+ const mod = _require(file)
440
+ return mod?.default ? mod.default : mod
441
+ }
442
+ const mod = await import(pathToFileURL(file).href)
443
+ return mod.default ?? mod
444
+ }
445
+
412
446
  /**
413
447
  * @internal
414
448
  * @private
package/src/converter.js CHANGED
@@ -26,7 +26,18 @@ import { TrailingDigitsRx } from './rx.js'
26
26
  export function applyBackendTraits(instance) {
27
27
  instance._backendTraits = null
28
28
 
29
- instance.basebackend = function (value = null) {
29
+ // Install a Ruby-style trait accessor method, but never clobber a flat string
30
+ // property the converter already declared (convention #2). Overwriting e.g.
31
+ // `converter.outfilesuffix = '.html'` with a method would silently turn the
32
+ // author's string into a function; the backend traits stay reachable through
33
+ // `_getBackendTraits()` instead.
34
+ const defineTraitAccessor = (name, fn) => {
35
+ const existing = Object.getOwnPropertyDescriptor(instance, name)
36
+ if (existing && typeof existing.value !== 'function') return
37
+ instance[name] = fn
38
+ }
39
+
40
+ defineTraitAccessor('basebackend', function (value = null) {
30
41
  if (value) {
31
42
  const traits = (this._backendTraits ??= {})
32
43
  traits.basebackend = value
@@ -40,19 +51,19 @@ export function applyBackendTraits(instance) {
40
51
  return value
41
52
  }
42
53
  return this._getBackendTraits().basebackend
43
- }
44
- instance.filetype = function (value = null) {
54
+ })
55
+ defineTraitAccessor('filetype', function (value = null) {
45
56
  if (value) return (this._getBackendTraits().filetype = value)
46
57
  return this._getBackendTraits().filetype
47
- }
48
- instance.htmlsyntax = function (value = null) {
58
+ })
59
+ defineTraitAccessor('htmlsyntax', function (value = null) {
49
60
  if (value) return (this._getBackendTraits().htmlsyntax = value)
50
61
  return this._getBackendTraits().htmlsyntax
51
- }
52
- instance.outfilesuffix = function (value = null) {
62
+ })
63
+ defineTraitAccessor('outfilesuffix', function (value = null) {
53
64
  if (value) return (this._getBackendTraits().outfilesuffix = value)
54
65
  return this._getBackendTraits().outfilesuffix
55
- }
66
+ })
56
67
  instance.supportsTemplates = function (value = true) {
57
68
  this._getBackendTraits().supportsTemplates = value
58
69
  }
@@ -136,7 +147,9 @@ export function normalizeConverter(converter, backend) {
136
147
  }
137
148
  }
138
149
 
139
- // Apply the BackendTraits mixin so Document can call the standard accessor methods.
150
+ // Apply the BackendTraits mixin so Document can read traits via
151
+ // _getBackendTraits(). Flat string properties (convention #2) are preserved:
152
+ // applyBackendTraits does not overwrite an existing same-named data property.
140
153
  applyBackendTraits(converter)
141
154
  if (traits) {
142
155
  converter._backendTraits = traits
package/src/document.js CHANGED
@@ -12,6 +12,7 @@
12
12
  // - Mutex / thread-safety not applicable in single-threaded JS.
13
13
  // - `instance_variable_get :@attribute_overrides` → direct property access.
14
14
 
15
+ import packageJson from '../package.json' with { type: 'json' }
15
16
  import { AbstractBlock } from './abstract_block.js'
16
17
  import { Section } from './section.js'
17
18
  import { Inline } from './inline.js'
@@ -400,7 +401,7 @@ export class Document extends AbstractBlock {
400
401
 
401
402
  const attrOverrides = this._attributeOverrides
402
403
  attrOverrides.asciidoctor = ''
403
- attrOverrides['asciidoctor-version'] = '3.0.0.dev' // matches Ruby VERSION
404
+ attrOverrides['asciidoctor-version'] = packageJson.version
404
405
 
405
406
  const safeModeName = SafeMode.nameForValue(this.safe)
406
407
  attrOverrides['safe-mode-name'] = safeModeName
@@ -592,16 +593,16 @@ export class Document extends AbstractBlock {
592
593
  // attribute (re)assignments are in scope; snapshot and restore the document
593
594
  // attributes so the downstream steps and conversion still start from the restored
594
595
  // (header) state, matching Ruby's restore_attributes-before-convert invariant.
596
+ //
597
+ // This runs in two passes because list item / table cell / dlist text may contain
598
+ // natural cross-references (e.g. <<Some section title>>) that resolve against the
599
+ // reftext→id map. Pass 1 substitutes titles and reftexts only, so every section
600
+ // reftext is known; the map is then built; pass 2 substitutes the block content
601
+ // text, where resolveId() can now match those natural references.
595
602
  const attributesSnapshot = { ...this.attributes }
596
- await this._resolveAllTexts(this)
597
- for (const key of Reflect.ownKeys(this.attributes))
598
- delete this.attributes[key]
599
- Object.assign(this.attributes, attributesSnapshot)
600
- // Reset the footnote counter so that body-content footnotes (processed during conversion)
601
- // start numbering from 1, reproducing Ruby's "out of sequence" quirk: title footnotes are
602
- // numbered during parsing via apply_title_subs, then the counter restarts for body content.
603
- delete this.attributes['footnote-number']
604
- delete this._counters['footnote-number']
603
+ // Pass 1: titles + reftexts (no block content text yet).
604
+ await this._resolveAllTexts(this, false)
605
+ this._restoreAttributeSnapshot(attributesSnapshot)
605
606
  // Pre-compute reftext for all registered inline anchor nodes.
606
607
  for (const ref of Object.values(this.catalog.refs)) {
607
608
  if (ref && typeof ref.precomputeReftext === 'function') {
@@ -610,6 +611,15 @@ export class Document extends AbstractBlock {
610
611
  }
611
612
  // Build the reftext→id lookup map so that resolveId() is synchronous.
612
613
  await this._buildReftextsMap()
614
+ // Pass 2: list item / table cell / dlist text, now that natural cross-references
615
+ // can be resolved against the reftext→id map.
616
+ await this._resolveAllTexts(this, true)
617
+ this._restoreAttributeSnapshot(attributesSnapshot)
618
+ // Reset the footnote counter so that body-content footnotes (processed during conversion)
619
+ // start numbering from 1, reproducing Ruby's "out of sequence" quirk: title footnotes are
620
+ // numbered during parsing via apply_title_subs, then the counter restarts for body content.
621
+ delete this.attributes['footnote-number']
622
+ delete this._counters['footnote-number']
613
623
 
614
624
  this._parsed = true
615
625
  return this
@@ -1584,6 +1594,10 @@ export class Document extends AbstractBlock {
1584
1594
  cell._innerDocument &&
1585
1595
  cell._innerContent == null
1586
1596
  ) {
1597
+ // Recurse into the inner document first so that AsciiDoc cells
1598
+ // of any nested table have their content computed before the
1599
+ // inner document (and the nested table) is rendered.
1600
+ await this._convertAsciiDocCells(cell._innerDocument)
1587
1601
  cell._innerContent = await cell._innerDocument.convert()
1588
1602
  }
1589
1603
  }
@@ -1664,12 +1678,33 @@ export class Document extends AbstractBlock {
1664
1678
  : ['attributes']
1665
1679
  }
1666
1680
 
1681
+ /**
1682
+ * @private
1683
+ * Restore the document attributes to a previously captured snapshot, discarding any
1684
+ * body-level (re)assignments replayed while pre-computing text. Mirrors Ruby's
1685
+ * restore_attributes-before-convert invariant.
1686
+ * @param {Object} snapshot - The attributes snapshot to restore.
1687
+ */
1688
+ _restoreAttributeSnapshot(snapshot) {
1689
+ for (const key of Reflect.ownKeys(this.attributes))
1690
+ delete this.attributes[key]
1691
+ Object.assign(this.attributes, snapshot)
1692
+ }
1693
+
1667
1694
  /**
1668
1695
  * @private
1669
1696
  * Walk the block tree and pre-compute all async text values.
1670
1697
  * Handles titles (AbstractBlock), list item text, table cell text, and reftexts.
1698
+ *
1699
+ * Runs in two passes (see {@link parse}): with `resolveContent` false only titles and
1700
+ * reftexts are substituted (so the reftext→id map can be built); with `resolveContent`
1701
+ * true the list item / table cell / dlist text is substituted, resolving any natural
1702
+ * cross-references against the now-complete map. Title/reftext pre-computation is
1703
+ * idempotent (results are cached), so running it in both passes is a no-op the second time.
1704
+ * @param {AbstractBlock} block - The block to resolve.
1705
+ * @param {boolean} resolveContent - Whether to substitute list item / cell / dlist text.
1671
1706
  */
1672
- async _resolveAllTexts(block) {
1707
+ async _resolveAllTexts(block, resolveContent) {
1673
1708
  // Replay this block's attribute entries (in document order, since the walk is
1674
1709
  // depth-first pre-order like conversion) so that body-level attribute assignments
1675
1710
  // and reassignments are in scope when the block's — and its descendants' and later
@@ -1695,12 +1730,12 @@ export class Document extends AbstractBlock {
1695
1730
  // dlist.blocks is an array of [[term, ...], item_or_null] pairs.
1696
1731
  for (const [terms, item] of block.blocks ?? []) {
1697
1732
  for (const term of terms ?? []) {
1698
- await term.precomputeText?.()
1699
- await this._resolveAllTexts(term)
1733
+ if (resolveContent) await term.precomputeText?.()
1734
+ await this._resolveAllTexts(term, resolveContent)
1700
1735
  }
1701
1736
  if (item) {
1702
- await item.precomputeText?.()
1703
- await this._resolveAllTexts(item)
1737
+ if (resolveContent) await item.precomputeText?.()
1738
+ await this._resolveAllTexts(item, resolveContent)
1704
1739
  }
1705
1740
  }
1706
1741
  } else if (ctx === 'table') {
@@ -1710,14 +1745,14 @@ export class Document extends AbstractBlock {
1710
1745
  ...(block.rows?.foot ?? []),
1711
1746
  ]) {
1712
1747
  for (const cell of row) {
1713
- await cell.precomputeText?.()
1748
+ if (resolveContent) await cell.precomputeText?.()
1714
1749
  await cell.precomputeReftext?.()
1715
1750
  }
1716
1751
  }
1717
1752
  } else {
1718
1753
  for (const child of block.blocks ?? []) {
1719
- await child.precomputeText?.()
1720
- await this._resolveAllTexts(child)
1754
+ if (resolveContent) await child.precomputeText?.()
1755
+ await this._resolveAllTexts(child, resolveContent)
1721
1756
  }
1722
1757
  }
1723
1758
  }
@@ -2009,14 +2044,19 @@ export class Document extends AbstractBlock {
2009
2044
  let newBasebackend, newFiletype
2010
2045
 
2011
2046
  if (converter && typeof converter._getBackendTraits === 'function') {
2012
- newBasebackend = converter.basebackend()
2013
- newFiletype = converter.filetype()
2014
- const htmlsyntax = converter.htmlsyntax()
2047
+ // Read the traits object directly rather than the same-named accessor
2048
+ // methods. A user converter that declares flat string properties
2049
+ // (`converter.outfilesuffix = '.html'`, convention #2) keeps them intact,
2050
+ // so callers reading `converter.outfilesuffix` still see the string.
2051
+ const traits = converter._getBackendTraits()
2052
+ newBasebackend = traits.basebackend
2053
+ newFiletype = traits.filetype
2054
+ const htmlsyntax = traits.htmlsyntax
2015
2055
  if (htmlsyntax) attrs.htmlsyntax = htmlsyntax
2016
2056
  if (init) {
2017
- attrs.outfilesuffix ??= converter.outfilesuffix()
2057
+ attrs.outfilesuffix ??= traits.outfilesuffix
2018
2058
  } else if (!this.isAttributeLocked('outfilesuffix')) {
2019
- attrs.outfilesuffix = converter.outfilesuffix()
2059
+ attrs.outfilesuffix = traits.outfilesuffix
2020
2060
  }
2021
2061
  } else if (converter) {
2022
2062
  const traits = deriveBackendTraits(newBackend)