Skip to content
New issue

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

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

Already on GitHub? # 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
Show file tree
Hide file tree
Changes from 4 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
56 changes: 55 additions & 1 deletion pages/11ty/markdown.shortcut.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ const color = require('kleur');
const {JSDOM} = require('jsdom');
const {markdown} = require('./markdown.lib');

const {github, rewriteRules, urlPrefix} = require('./site.config');
const {github, rewriteRules, urlPrefix, globalTerms} = require('./site.config');

class MDRenderer {
static async render(filePath, startAnchor, endAnchor) {
try {
const content = await MDRenderer.parseFile(filePath);
const {window} = new JSDOM(content);
const localTerms = MDRenderer.generateHeadersIds(window.document.body);
const terms = Object.assign({}, globalTerms, localTerms);

// Exclude part before start anchor
if (startAnchor) {
Expand All @@ -27,6 +29,9 @@ class MDRenderer {
endAnchorElement.remove();
}

// Add anchors and globally defined terms links
MDRenderer.fillReferenceLinks(window.document,window.document.body, terms);

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

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

static generateHeadersIds(content) {
const headers = [...content.querySelectorAll('h1, h2, h3, h4')];
const localTerms = new Map();
for (const header of headers) {
const text = header.textContent;
const id = MDRenderer.createIDFromText(text)
header.setAttribute('id', id);
const anchor = `#${id}`
localTerms.set(text, anchor);
}
return localTerms
}
static createIDFromText(text, idLengthLimit = 20) {
return 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);
}

static findTextNodes(root) {
const all = [];
for (let node = root.firstChild; node; node = node.nextSibling) {
if (node.nodeType === 3) all.push(node);
else all.push(...MDRenderer.findTextNodes(node));
}
return all
}

static fillReferenceLinks(document, content, terms) {
const nodes = MDRenderer.findTextNodes(content)
.filter((node) => !node.parentElement.closest('h1, h2, h3, h4, h5, h6'))

for (const [text, link] of Object.entries(terms)) {

for (const node of nodes) {
if (node.textContent.includes(text)) {
MDRenderer.wrapTextNode(document, node, text, link)
}
}
}
}

static wrapTextNode(document, node, text, link) {
const wrapper = document.createElement('span');
const regex = new RegExp(`(^|\\s)${text}(,?\\s|\\.?\\s)`, 'g');
wrapper.innerHTML = node.textContent.replace(regex, `$1<a href="${link}">${text}</a>$2`);
return node.replaceWith(...wrapper.childNodes);
}
}

module.exports = (config) => {
Expand Down
4 changes: 4 additions & 0 deletions pages/site.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ rewriteRules:
"src/modules/esl-traversing-query/README.md": "/core/esl-traversing-query"
"src/modules/esl-trigger/README.md": "/components/esl-trigger"
"src/modules/esl-toggleable/README.md": "/components/esl-toggleable"

globalTerms:
"ESL Base Element": "/core/esl-base-element"
"ESL Mixin Element": "/core/esl-mixin-element"