Code cleanup for URL post-processing.
This commit is contained in:
parent
4324ad3443
commit
1fccac62c4
1 changed files with 40 additions and 62 deletions
|
|
@ -1,13 +1,35 @@
|
|||
import { visit } from 'unist-util-visit';
|
||||
|
||||
let currentBook = '';
|
||||
|
||||
function scriptureLink(node, args) {
|
||||
const value = node.children?.[0]?.value.trim() || '';
|
||||
let passage = (args && args.length > 0 && args[0] != '') ? args[0] : value;
|
||||
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 => {
|
||||
let currentBook = '';
|
||||
|
||||
visit(tree, 'element', (node) => {
|
||||
let passage = '';
|
||||
let isQuote = false;
|
||||
let isCite = false;
|
||||
let tag = '';
|
||||
let rest = '';
|
||||
const url = node.properties.href;
|
||||
|
|
@ -18,82 +40,38 @@ export const ScriptureLinks = () => {
|
|||
return;
|
||||
}
|
||||
if (url.indexOf('=') != -1) {
|
||||
tag = url.substring(0, url.indexOf('='));
|
||||
rest = url.substring(url.indexOf('='));
|
||||
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(','));
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
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();
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
Loading…
Reference in a new issue