Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #4 node.getContext() is not a function #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 27 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,35 @@ 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)
if (Array.isArray(node)) {
// e.g. dlist
for (let i = 0; i < node.length; i++) {
if (node[i] !== null) {
processNode(src, node[i], lookup)
}
}
return
}

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.')
}
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,87 @@ describe('xref extension', () => {
expect(loggerDestination.messages).to.be.empty()
})

it('should allow reference in dlist items', () => {
const extensionConfig = {
logUnnecessaryLinkTextWarnings: false,
}
addFile(
'target.adoc',
heredoc`
= Target

[[frag]]
== Fragment
The Fragment
`
)
addFile(
'xref.adoc',
heredoc`
= Dlist

Simple::

Simple1::
xref:target.adoc#frag[Existing]

Simple2:: xref:target.adoc#frag[Existing]

xref:target.adoc#frag[Existing]:: Also works

// These do not work, yet :(
// Numbered::
// . xref:target.adoc#frag[Existing]

//
// Outer::
// Inner:::
// xref:target.adoc#frag[Existing]
`
)
run(extensionConfig)
// console.log(loggerDestination.messages)
const page = contentCatalog.getPages((candidate) => candidate.path === '/xref.adoc')[0]
expect(loggerDestination.messages).to.be.empty()
})

it('should log warnings for dangling references from dlist items', () => {
const extensionConfig = {
logUnnecessaryLinkTextWarnings: false,
}
addFile(
'target.adoc',
heredoc`
= Target

[[frag]]
== Fragment
The Fragment
`
)
addFile(
'xref.adoc',
heredoc`
= Dlist

Simple1::
xref:target.adoc#dangling1[Dangling]

Simple2:: xref:target.adoc#dangling2[Dangling]

xref:target.adoc#dangling3[Dangling]:: Also works
`
)
run(extensionConfig)
const page = contentCatalog.getPages((candidate) => candidate.path === '/xref.adoc')[0]
expect(
loggerDestination.messages.length == 3 &&
loggerDestination.messages.every(
(message) => message.includes('"level":"error"') && message.includes('target fragement of xref not found: target.adoc#dangling')
)
).to.be.true()
})

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