notes/src/utils/rehype.js

71 lines
2.3 KiB
JavaScript
Raw Normal View History

import { visit } from 'unist-util-visit';
export const ScriptureLinks = () => {
return async tree => {
let currentBook = '';
visit(tree, 'element', (node) => {
let passage = '';
let isQuote = false;
2026-06-11 17:58:57 -06:00
let isCite = false;
const url = node.properties.href;
if (node.tagName !== 'a') {
return;
}
if (!url.startsWith(':scripture')) {
return;
}
if (url.indexOf('=') != -1) {
const args = url.substring(url.indexOf('=')+1).split(',');
for (const arg of args) {
if (arg === 'quote') {
isQuote = true;
2026-06-11 17:58:57 -06:00
} else if (arg === 'cite') {
isCite = true;
} else {
passage = arg;
}
}
} else {
passage = node.children?.[0]?.value.trim() || '';
}
2026-06-11 22:32:35 -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+?/)
2026-06-11 22:32:35 -06:00
passage = passage.replaceAll(/ /g, '+');
passage = passage.replace(/\:/, '%3A');
2026-06-11 17:58:57 -06:00
if (passage !== '') {
node.properties.href = `https://www.biblegateway.com/passage/?search=${passage}&version=ESV`;
}
2026-06-11 00:02:58 -06:00
node.properties.class = 'scripture';
2026-06-11 00:04:52 -06:00
if (isQuote) {
if (passage === '') {
node.properties.href = null;
node.properties.title = 'Scriptural citation or fragmentary citation';
}
2026-06-11 17:58:57 -06:00
node.properties.class += ' quote';
}
if (isCite) {
if (passage === '') {
node.properties.href = null;
node.properties.title = 'Scriptural citation or fragmentary citation';
}
node.properties.class += ' cite';
2026-06-11 00:04:52 -06:00
}
2026-06-11 00:02:58 -06:00
if (match) {
currentBook = match?.[1].trim();
}
});
}
};