notes/src/utils/rehype.js

71 lines
No EOL
2.3 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';
export const ScriptureLinks = () => {
return async tree => {
let currentBook = '';
visit(tree, 'element', (node) => {
let passage = '';
let isQuote = false;
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;
} else if (arg === 'cite') {
isCite = true;
} else {
passage = arg;
}
}
} else {
passage = node.children?.[0]?.value.trim() || '';
}
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.class = 'scripture';
if (isQuote) {
if (passage === '') {
node.properties.href = null;
node.properties.title = 'Scriptural citation or fragmentary citation';
}
node.properties.class += ' quote';
}
if (isCite) {
if (passage === '') {
node.properties.href = null;
node.properties.title = 'Scriptural citation or fragmentary citation';
}
node.properties.class += ' cite';
}
if (match) {
currentBook = match?.[1].trim();
}
});
}
};