Skip to content

Commit

Permalink
Set anchors to term titles (#76)
Browse files Browse the repository at this point in the history
* Set anchors to term title

* Apply suggestions from code review
  • Loading branch information
JakeSCahill authored Sep 12, 2024
1 parent 1759a70 commit a96a35f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
23 changes: 17 additions & 6 deletions macros/glossary.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ module.exports.register = function (registry, config = {}) {

const terms = termFiles.map(file => {
const content = file.contents.toString()
// Split content by lines and get the first non-empty line as the title
const lines = content.split('\n').map(line => line.trim())
const firstNonEmptyLine = lines.find(line => line.length > 0)
// Remove leading '=' characters (AsciiDoc syntax) and trim whitespace
const pageTitle = firstNonEmptyLine ? firstNonEmptyLine.replace(/^=+\s*/, '') : '#'
const attributes = {}

let match
Expand All @@ -50,6 +55,7 @@ module.exports.register = function (registry, config = {}) {
term: attributes['term-name'],
def: attributes['hover-text'],
category: attributes['category'] || '',
pageTitle,
content
}

Expand All @@ -76,13 +82,16 @@ module.exports.register = function (registry, config = {}) {
}
}

//characters to replace by '-' in generated idprefix
const IDRX = /[/ _.-]+/g
// Characters to replace by '-' in generated idprefix
const IDRX = /[\/ _.-]+/g

function termId (term) {
return term.toLowerCase().replace(IDRX, '-')
function termId(term) {
// Remove brackets before replacing other characters
const noBracketsTerm = term.replace(/[\[\]\(\)]/g, '') // Remove brackets
return noBracketsTerm.toLowerCase().replace(IDRX, '-')
}


const TRX = /(<[a-z]+)([^>]*>.*)/

function glossaryInlineMacro () {
Expand All @@ -91,7 +100,7 @@ module.exports.register = function (registry, config = {}) {
self.named('glossterm')
//Specifying the regexp allows spaces in the term.
self.$option('regexp', /glossterm:([^[]+)\[(|.*?[^\\])\]/)
self.positionalAttributes(['definition', 'customText']);
self.positionalAttributes(['definition', 'customText']); // Allows for specifying custom link text
self.process(function (parent, target, attributes) {
const term = attributes.term || target
const customText = attributes.customText || term;
Expand All @@ -110,9 +119,11 @@ module.exports.register = function (registry, config = {}) {
}
const logTerms = document.hasAttribute('glossary-log-terms')
var definition;
var pageTitle;
const index = context.gloss.findIndex((candidate) => candidate.term === term)
if (index >= 0) {
definition = context.gloss[index].def
pageTitle = context.gloss[index].pageTitle
} else {
definition = attributes.definition;
}
Expand All @@ -138,7 +149,7 @@ module.exports.register = function (registry, config = {}) {
if ((termExistsInContext && links) || (links && customLink)) {
inline = customLink
? self.createInline(parent, 'anchor', customText, { type: 'link', target: customLink, attributes: { ...attrs, window: '_blank', rel: 'noopener noreferrer' } })
: self.createInline(parent, 'anchor', customText, { type: 'xref', target: `${glossaryPage}#${termId(term)}`, reftext: customText, attributes: attrs })
: self.createInline(parent, 'anchor', customText, { type: 'xref', target: `${glossaryPage}#${termId(pageTitle)}`, reftext: customText, attributes: attrs })
} else {
inline = self.createInline(parent, 'quoted', customText, { attributes: attrs })
}
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@redpanda-data/docs-extensions-and-macros",
"version": "3.6.5",
"version": "3.6.6",
"description": "Antora extensions and macros developed for Redpanda documentation.",
"keywords": [
"antora",
Expand Down

0 comments on commit a96a35f

Please sign in to comment.