0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2025-12-27 02:52:43 +00:00

Updated and reworked to handle more definition*

Updated to allow multiple definition terms and definitions per term

<Term>::<definition>
<Term>::<definition1>::<definition2>
::<definition3>

```
**Example** ::
::V3 uses HTML *definition lists* to create "lists" with hanging indents.
::Three
I'm a term::Four

**<u>Hello</u>**::I\'m a different
::List
:
```
This commit is contained in:
David Bolack
2023-12-20 22:57:37 -06:00
parent eeba037244
commit 96d973528c
3 changed files with 42 additions and 23 deletions

View File

@@ -237,32 +237,45 @@ const superSubScripts = {
const definitionLists = {
name : 'definitionLists',
level : 'block',
start(src) { return src.match(/^.*?::.*/m)?.index; }, // Hint to Marked.js to stop and check for a match
start(src) { return src.match(/^.*?::.*\n\n/m)?.index; }, // Hint to Marked.js to stop and check for a match
tokenizer(src, tokens) {
const regex = /^([^\n]*?)::([^\n]*)(?:\n|$)/ym;
const regex = /^([^\n:]*?)::(.*)(?:\n|$)/ym;
let match;
let endIndex = 0;
const definitions = [];
const endIndex = src.match(`\n\n`)?.index + 2;
const allDefinitions = [];
let currentDefinition = [];
while (match = regex.exec(src)) {
definitions.push({
dt : this.lexer.inlineTokens(match[1].trim()),
dd : match[2].split('::').map((s)=>this.lexer.inlineTokens(s.trim()))
});
endIndex = regex.lastIndex;
if(match[1].trim()?.length) {
if(currentDefinition?.dt?.length) {
allDefinitions.push(currentDefinition);
currentDefinition = [];
}
currentDefinition = {
dt : this.lexer.inlineTokens(match[1].trim()),
dd : []
};
}
currentDefinition.dd = currentDefinition.dd.concat(match[2].split('::').filter((item)=>item).map((s)=>this.lexer.inlineTokens(s.trim())));
if(!currentDefinition.dd?.length) {
currentDefinition.dd = [this.lexer.inlineTokens('')];
}
}
if(definitions.length) {
if(currentDefinition.hasOwnProperty('dt')) { allDefinitions.push(currentDefinition); }
if(allDefinitions.length) {
return {
type : 'definitionLists',
raw : src.slice(0, endIndex),
definitions
type : 'definitionLists',
raw : src.slice(0, endIndex),
definitions : allDefinitions
};
}
},
renderer(token) {
return `<dl>${token.definitions.reduce((html, def)=>{
let returnVal = `<dl>`;
token.definitions.forEach((def)=>{
const dds = def.dd.map((s)=>`<dd>${this.parser.parseInline(s)}</dd>`).join('\n');
return `${html}<dt>${this.parser.parseInline(def.dt)}</dt>${dds}`;
}, '')}</dl>`;
returnVal += `<div><dt>${this.parser.parseInline(def.dt)}</dt>${dds}</div>`;
});
return `${returnVal}</dl>`;
}
};