From 1df1bb361e6d82955f78e33befcb5f0f293d5f61 Mon Sep 17 00:00:00 2001 From: "Benjamin A. Shelton" Date: Thu, 20 Aug 2026 15:00:55 -0600 Subject: [PATCH] Move link parsing to its own class. This allows finer granularity and control of current book and link parsing (and allows us to give unique names to anchors per-page). --- src/utils/rehype.js | 71 ++++++++++++++++++++++++++------------------- 1 file changed, 41 insertions(+), 30 deletions(-) diff --git a/src/utils/rehype.js b/src/utils/rehype.js index cd69372..7547fa7 100644 --- a/src/utils/rehype.js +++ b/src/utils/rehype.js @@ -1,48 +1,59 @@ import { visit } from 'unist-util-visit'; -let currentBook = ''; +class LinkParser { + linkCount = 1; + currentBook = ''; -function scriptureLink(node, args, meta) { - const value = node.children?.[0]?.value?.trim() || ''; - let passage = (args && args.length > 0 && args[0] != '') ? args[0] : value; + constructor() { + this.linkCount = 1; + this.currentBook = ''; + } - if (value.match(/^[Vv][Vv]?(erses?)?\s*/)) { - const book = meta.data.astro.frontmatter.book; - const chapter = meta.data.astro.frontmatter.chapter; - if ((args.length == 0 || args[0] == '') && (book && chapter)) { - passage = `${book} ${chapter}:${value.replace(/^[Vv][Vv]?(erses?)?\s*/, '')}`; - } else if (args.length == 0 || args[0] == '') { - node.properties.href = null; - return; + parse(node, args, meta) { + const value = node.children?.[0]?.value?.trim() || ''; + let passage = (args && args.length > 0 && args[0] != '') ? args[0] : value; + + if (value.match(/^[Vv][Vv]?(erses?)?\s*/)) { + const book = meta.data.astro.frontmatter.book; + const chapter = meta.data.astro.frontmatter.chapter; + if ((args.length == 0 || args[0] == '') && (book && chapter)) { + passage = `${book} ${chapter}:${value.replace(/^[Vv][Vv]?(erses?)?\s*/, '')}`; + } else if (args.length == 0 || args[0] == '') { + node.properties.href = null; + return; + } } - } - // Stylized hyphen replacement from Logos. - passage = passage.replaceAll(/–/g, '-'); + // Stylized hyphen replacement from Logos. + passage = passage.replaceAll(/–/g, '-'); - if (passage.match(/^[-0-9:]+$/)) { - passage = `${currentBook} ${passage}`; - } + if (passage.match(/^[-0-9:]+$/)) { + passage = `${this.currentBook} ${passage}`; + } - passage = passage.toLowerCase(); - passage = passage.replace(/pss/, 'ps'); - const match = passage.match(/^(\d?\s*[-a-zA-Z]+)\s*\d+?/) + passage = passage.toLowerCase(); + passage = passage.replace(/pss/, 'ps'); + const match = passage.match(/^(\d?\s*[-a-zA-Z]+)\s*\d+?/) - passage = passage.replaceAll(/ /g, '+'); - passage = passage.replace(/\:/, '%3A'); + passage = passage.replaceAll(/ /g, '+'); + passage = passage.replace(/\:/, '%3A'); - if (passage !== '') { - node.properties.href = `https://www.biblegateway.com/passage/?search=${passage}&version=ESV`; - node.properties["data-scripture"] = passage; - } + if (passage !== '') { + node.properties.href = `https://www.biblegateway.com/passage/?search=${passage}&version=ESV`; + node.properties["data-scripture"] = passage; + node.properties.name = 'scripture-'+this.linkCount; + this.linkCount++; + } - if (match) { - currentBook = match?.[1].trim(); + if (match) { + this.currentBook = match?.[1].trim(); + } } } export const ScriptureLinks = () => { return async (tree, file) => { + const parser = new LinkParser(); visit(tree, 'element', (node) => { let tag = ''; let rest = ''; @@ -64,7 +75,7 @@ export const ScriptureLinks = () => { switch (tag) { // Scripture reference. case 'scripture': - scriptureLink(node, rest.split(','), file); + parser.parse(node, rest.split(','), file); break; // Quoting Jesus' words. case 'jesus':