Move link parsing to its own class.
This allows finer granularity and control of current book and link parsing (and allows us to give unique names to anchors per-page).
This commit is contained in:
parent
13e66e4f93
commit
1df1bb361e
1 changed files with 41 additions and 30 deletions
|
|
@ -1,48 +1,59 @@
|
|||
import { visit } from 'unist-util-visit';
|
||||
|
||||
let currentBook = '';
|
||||
class LinkParser {
|
||||
linkCount = 1;
|
||||
currentBook = '';
|
||||
|
||||
function scriptureLink(node, args, meta) {
|
||||
const value = node.children?.[0]?.value?.trim() || '';
|
||||
let passage = (args && args.length > 0 && args[0] != '') ? args[0] : value;
|
||||
constructor() {
|
||||
this.linkCount = 1;
|
||||
this.currentBook = '';
|
||||
}
|
||||
|
||||
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;
|
||||
parse(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, '-');
|
||||
// Stylized hyphen replacement from Logos.
|
||||
passage = passage.replaceAll(/–/g, '-');
|
||||
|
||||
if (passage.match(/^[-0-9:]+$/)) {
|
||||
passage = `${currentBook} ${passage}`;
|
||||
}
|
||||
if (passage.match(/^[-0-9:]+$/)) {
|
||||
passage = `${this.currentBook} ${passage}`;
|
||||
}
|
||||
|
||||
passage = passage.toLowerCase();
|
||||
passage = passage.replace(/pss/, 'ps');
|
||||
const match = passage.match(/^(\d?\s*[-a-zA-Z]+)\s*\d+?/)
|
||||
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');
|
||||
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 (passage !== '') {
|
||||
node.properties.href = `https://www.biblegateway.com/passage/?search=${passage}&version=ESV`;
|
||||
node.properties["data-scripture"] = passage;
|
||||
node.properties.name = 'scripture-'+this.linkCount;
|
||||
this.linkCount++;
|
||||
}
|
||||
|
||||
if (match) {
|
||||
currentBook = match?.[1].trim();
|
||||
if (match) {
|
||||
this.currentBook = match?.[1].trim();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const ScriptureLinks = () => {
|
||||
return async (tree, file) => {
|
||||
const parser = new LinkParser();
|
||||
visit(tree, 'element', (node) => {
|
||||
let tag = '';
|
||||
let rest = '';
|
||||
|
|
@ -64,7 +75,7 @@ export const ScriptureLinks = () => {
|
|||
switch (tag) {
|
||||
// Scripture reference.
|
||||
case 'scripture':
|
||||
scriptureLink(node, rest.split(','), file);
|
||||
parser.parse(node, rest.split(','), file);
|
||||
break;
|
||||
// Quoting Jesus' words.
|
||||
case 'jesus':
|
||||
|
|
|
|||
Loading…
Reference in a new issue