Skip to content

Commit

Permalink
Fixes #4 node.getContext() is not a function
Browse files Browse the repository at this point in the history
caused by unexpcted double colons in asciidoc content e.g. like this:

gRPC port:: 11004
  • Loading branch information
cpfeiffer committed Jul 29, 2024
1 parent ae6d1ad commit f949dca
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
30 changes: 17 additions & 13 deletions packages/antora-xref-extension/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,25 @@ function register ({ config }) {
}

function processNode (src, node, lookup) {
const context = node.getContext()
if (context === 'paragraph' || context === 'admonition') {
const lines = node.lines
for (let i = 0; i < lines.length; i++) {
lines[i] = processLine(src, node, lines[i], lookup)
try {
const context = node.getContext()
if (context === 'paragraph' || context === 'admonition') {
const lines = node.lines
for (let i = 0; i < lines.length; i++) {
lines[i] = processLine(src, node, lines[i], lookup)
}
} else if (context === 'table') {
const rows = node.getRows()
rows.getHead().forEach((row) => row.forEach((cell) => processNode(src, cell, lookup)))
rows.getBody().forEach((row) => row.forEach((cell) => processNode(src, cell, lookup)))
rows.getFoot().forEach((row) => row.forEach((cell) => processNode(src, cell, lookup)))
} else if (context === 'table_cell' || context === 'list_item') {
node.text = processLine(src, node, node.text, lookup)
}
} else if (context === 'table') {
const rows = node.getRows()
rows.getHead().forEach((row) => row.forEach((cell) => processNode(src, cell, lookup)))
rows.getBody().forEach((row) => row.forEach((cell) => processNode(src, cell, lookup)))
rows.getFoot().forEach((row) => row.forEach((cell) => processNode(src, cell, lookup)))
} else if (context === 'table_cell' || context === 'list_item') {
node.text = processLine(src, node, node.text, lookup)
node.getBlocks().forEach((child) => processNode(src, child, lookup))
} catch (t) {
log(node, 'warn', 'Parse error when validating xrefs. Check e.g. for double colons (\'::\')')
}
node.getBlocks().forEach((child) => processNode(src, child, lookup))
}

function processLine (src, block, line, lookup) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,27 @@ describe('xref extension', () => {
expect(loggerDestination.messages).to.be.empty()
})

it('should not fail but log a warning on unexpected syntax', () => {
const extensionConfig = {
logUnnecessaryLinkTextWarnings: false,
}
addFile(
'bad.adoc',
heredoc`
= Bad
bad::
`
)
run(extensionConfig)
const page = contentCatalog.getPages((candidate) => candidate.path === '/bad.adoc')[0]
expect(
loggerDestination.messages.some(
(message) => message.includes('"level":"warn"') && message.includes('Parse error when validating xrefs.')
)
).to.be.true()
})

function addFile (filename, contents) {
contents = Buffer.from(contents)
const mediaType = 'text/asciidoc'
Expand Down

0 comments on commit f949dca

Please sign in to comment.