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

docs(site) internal page anchors and referencing mechanism automation #1820

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from 2 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
53 changes: 53 additions & 0 deletions pages/11ty/markdown.shortcut.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ class MDRenderer {
endAnchorElement.remove();
}

//Add headers ids
MDRenderer.generateHeadersIds(window.document.body);

// Add globally defined terms links
MDRenderer.generateGloballyDefinedTermsLinks(window.document.body);

// Resolve content links
MDRenderer.resolveLinks(window.document.body, filePath);

Expand Down Expand Up @@ -71,6 +77,53 @@ class MDRenderer {
}
return github.srcUrl + linkPath;
}

static generateHeadersIds(content) {
const headerTags = ['h1', 'h2', 'h3', 'h4'];
const idLengthLimit = 20;

headerTags.forEach(tag => {
const headers = content.getElementsByTagName(tag);

for (let header of headers) {
const text = header.textContent;
const id = text
.replace(/(?:^\w|[A-Z]|\b\w)/g, (word, index) => (index === 0 ? word.toLowerCase() : word.toUpperCase()))
.replace(/[^\w\s]|_/g, "").replace(/\s+/g, "")
.substring(0, idLengthLimit);

header.setAttribute('id', id);

this.generateAnchors(content, text, `#${id}`)
}
});
HenadzV marked this conversation as resolved.
Show resolved Hide resolved
return content;
}

static generateGloballyDefinedTermsLinks (content) {
const globallyDefinedTerms = {
ESL_Base_Element: 'https://esl-ui.com/core/esl-base-element/',
ESL_Mixin_Element: 'https://esl-ui.com/core/esl-mixin-element/'
// Add other globally defined terms as needed
};
HenadzV marked this conversation as resolved.
Show resolved Hide resolved

Object.keys(globallyDefinedTerms).forEach(term => {
this.generateAnchors(content, term.replace(/_/g, " "), globallyDefinedTerms[term]);
HenadzV marked this conversation as resolved.
Show resolved Hide resolved
});
}

static generateAnchors(content, text, link) {
HenadzV marked this conversation as resolved.
Show resolved Hide resolved
const matches = Array.from(content.querySelectorAll('*'))
.filter(element => element.textContent.includes(text) || element.textContent.includes(text.replace(/_/g, " ")));
const regex = new RegExp(`(^|\\s)${text}(\\s|[,\\.])`, 'g');

matches.forEach(match => {
if (!['H1', 'H2', 'H3', 'H4', 'H5', 'H6'].includes(match.tagName)) {
match.innerHTML = match.innerHTML.replace(regex, `$1<a href="${link}">${text}</a>$2`);
}
});
}

}

module.exports = (config) => {
Expand Down