notes/src/utils/rehype.js

90 lines
3 KiB
JavaScript
Raw Normal View History

import { visit } from 'unist-util-visit';
2026-06-29 20:06:57 -06:00
let currentBook = '';
function scriptureLink(node, args, meta) {
2026-06-29 20:31:36 -06:00
const value = node.children?.[0]?.value?.trim() || '';
2026-06-29 20:06:57 -06:00
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.
2026-06-29 20:06:57 -06:00
passage = passage.replaceAll(/–/g, '-');
if (passage.match(/^[-0-9:]+$/)) {
passage = `${currentBook} ${passage}`;
}
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');
if (passage !== '') {
node.properties.href = `https://www.biblegateway.com/passage/?search=${passage}&version=ESV`;
}
if (match) {
currentBook = match?.[1].trim();
}
}
export const ScriptureLinks = () => {
return async (tree, file) => {
visit(tree, 'element', (node) => {
2026-06-28 16:45:50 -06:00
let tag = '';
2026-06-29 16:13:41 -06:00
let rest = '';
const url = node.properties.href;
if (node.tagName !== 'a') {
return;
}
2026-06-28 16:45:50 -06:00
if (!url.startsWith(':')) {
return;
}
if (url.indexOf('=') != -1) {
2026-06-29 20:06:57 -06:00
tag = url.substring(1, url.indexOf('='));
rest = url.substring(url.indexOf('=')+1);
2026-06-28 16:45:50 -06:00
} else {
tag = url.substring(1);
}
2026-06-29 20:06:57 -06:00
node.properties.class = 'scripture';
2026-06-29 16:13:41 -06:00
switch (tag) {
// Scripture reference.
case 'scripture':
scriptureLink(node, rest.split(','), file);
2026-06-29 16:13:41 -06:00
break;
// Quoting Jesus' words.
case 'jesus':
2026-06-29 20:06:57 -06:00
case 'christ':
node.properties.href = null;
node.properties.title = 'Scriptural citation or fragmentary citation';
node.properties.class += ' christ';
2026-06-29 16:13:41 -06:00
break;
// Bible quote.
case 'quote':
2026-06-29 20:06:57 -06:00
node.properties.href = null;
node.properties.title = 'Scriptural citation or fragmentary citation';
node.properties.class += ' quote';
2026-06-29 16:13:41 -06:00
break;
// Source citation.
case 'cite':
2026-06-29 20:06:57 -06:00
node.properties.href = null;
node.properties.title = 'Scriptural citation or fragmentary citation';
node.properties.class += ' citation';
2026-06-29 16:13:41 -06:00
break;
}
});
}
};