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';
|
import { visit } from 'unist-util-visit';
|
||||||
|
|
||||||
let currentBook = '';
|
class LinkParser {
|
||||||
|
linkCount = 1;
|
||||||
|
currentBook = '';
|
||||||
|
|
||||||
function scriptureLink(node, args, meta) {
|
constructor() {
|
||||||
const value = node.children?.[0]?.value?.trim() || '';
|
this.linkCount = 1;
|
||||||
let passage = (args && args.length > 0 && args[0] != '') ? args[0] : value;
|
this.currentBook = '';
|
||||||
|
}
|
||||||
|
|
||||||
if (value.match(/^[Vv][Vv]?(erses?)?\s*/)) {
|
parse(node, args, meta) {
|
||||||
const book = meta.data.astro.frontmatter.book;
|
const value = node.children?.[0]?.value?.trim() || '';
|
||||||
const chapter = meta.data.astro.frontmatter.chapter;
|
let passage = (args && args.length > 0 && args[0] != '') ? args[0] : value;
|
||||||
if ((args.length == 0 || args[0] == '') && (book && chapter)) {
|
|
||||||
passage = `${book} ${chapter}:${value.replace(/^[Vv][Vv]?(erses?)?\s*/, '')}`;
|
if (value.match(/^[Vv][Vv]?(erses?)?\s*/)) {
|
||||||
} else if (args.length == 0 || args[0] == '') {
|
const book = meta.data.astro.frontmatter.book;
|
||||||
node.properties.href = null;
|
const chapter = meta.data.astro.frontmatter.chapter;
|
||||||
return;
|
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.
|
// Stylized hyphen replacement from Logos.
|
||||||
passage = passage.replaceAll(/–/g, '-');
|
passage = passage.replaceAll(/–/g, '-');
|
||||||
|
|
||||||
if (passage.match(/^[-0-9:]+$/)) {
|
if (passage.match(/^[-0-9:]+$/)) {
|
||||||
passage = `${currentBook} ${passage}`;
|
passage = `${this.currentBook} ${passage}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
passage = passage.toLowerCase();
|
passage = passage.toLowerCase();
|
||||||
passage = passage.replace(/pss/, 'ps');
|
passage = passage.replace(/pss/, 'ps');
|
||||||
const match = passage.match(/^(\d?\s*[-a-zA-Z]+)\s*\d+?/)
|
const match = passage.match(/^(\d?\s*[-a-zA-Z]+)\s*\d+?/)
|
||||||
|
|
||||||
passage = passage.replaceAll(/ /g, '+');
|
passage = passage.replaceAll(/ /g, '+');
|
||||||
passage = passage.replace(/\:/, '%3A');
|
passage = passage.replace(/\:/, '%3A');
|
||||||
|
|
||||||
if (passage !== '') {
|
if (passage !== '') {
|
||||||
node.properties.href = `https://www.biblegateway.com/passage/?search=${passage}&version=ESV`;
|
node.properties.href = `https://www.biblegateway.com/passage/?search=${passage}&version=ESV`;
|
||||||
node.properties["data-scripture"] = passage;
|
node.properties["data-scripture"] = passage;
|
||||||
}
|
node.properties.name = 'scripture-'+this.linkCount;
|
||||||
|
this.linkCount++;
|
||||||
|
}
|
||||||
|
|
||||||
if (match) {
|
if (match) {
|
||||||
currentBook = match?.[1].trim();
|
this.currentBook = match?.[1].trim();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const ScriptureLinks = () => {
|
export const ScriptureLinks = () => {
|
||||||
return async (tree, file) => {
|
return async (tree, file) => {
|
||||||
|
const parser = new LinkParser();
|
||||||
visit(tree, 'element', (node) => {
|
visit(tree, 'element', (node) => {
|
||||||
let tag = '';
|
let tag = '';
|
||||||
let rest = '';
|
let rest = '';
|
||||||
|
|
@ -64,7 +75,7 @@ export const ScriptureLinks = () => {
|
||||||
switch (tag) {
|
switch (tag) {
|
||||||
// Scripture reference.
|
// Scripture reference.
|
||||||
case 'scripture':
|
case 'scripture':
|
||||||
scriptureLink(node, rest.split(','), file);
|
parser.parse(node, rest.split(','), file);
|
||||||
break;
|
break;
|
||||||
// Quoting Jesus' words.
|
// Quoting Jesus' words.
|
||||||
case 'jesus':
|
case 'jesus':
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue