notes/src/utils/rehype.js

91 lines
No EOL
3.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { visit } from 'unist-util-visit';
let currentBook = '';
function scriptureLink(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, '-');
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`;
node.properties["data-scripture"] = passage;
}
if (match) {
currentBook = match?.[1].trim();
}
}
export const ScriptureLinks = () => {
return async (tree, file) => {
visit(tree, 'element', (node) => {
let tag = '';
let rest = '';
const url = node.properties.href;
if (node.tagName !== 'a') {
return;
}
if (!url.startsWith(':')) {
return;
}
if (url.indexOf('=') != -1) {
tag = url.substring(1, url.indexOf('='));
rest = url.substring(url.indexOf('=')+1);
} else {
tag = url.substring(1);
}
node.properties.class = 'scripture';
switch (tag) {
// Scripture reference.
case 'scripture':
scriptureLink(node, rest.split(','), file);
break;
// Quoting Jesus' words.
case 'jesus':
case 'christ':
node.properties.href = null;
node.properties.title = 'Scriptural citation or fragmentary citation';
node.properties.class += ' christ';
break;
// Bible quote.
case 'quote':
node.properties.href = null;
node.properties.title = 'Scriptural citation or fragmentary citation';
node.properties.class += ' quote';
break;
// Source citation.
case 'cite':
node.properties.href = null;
node.properties.title = 'Scriptural citation or fragmentary citation';
node.properties.class += ' citation';
break;
}
});
}
};