mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2025-12-24 16:22:44 +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:
@@ -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>`;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -4,21 +4,27 @@ const Markdown = require('naturalcrit/markdown.js');
|
||||
|
||||
describe('Dictionary Terms', ()=>{
|
||||
test('Single Definition', function() {
|
||||
const source = 'My term :: My First Definition';
|
||||
const source = 'My term :: My First Definition\n\n';
|
||||
const rendered = Markdown.render(source);
|
||||
expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('<dl><dt>My term</dt><dd>My First Definition</dd></dl>');
|
||||
expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('<dl><div><dt>My term</dt><dd>My First Definition</dd></div></dl>');
|
||||
});
|
||||
|
||||
test('Two Definitions', function() {
|
||||
const source = 'My term :: My First Definition :: My Second Definition';
|
||||
const source = 'My term :: My First Definition :: My Second Definition\n\n';
|
||||
const rendered = Markdown.render(source);
|
||||
expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('<dl><dt>My term</dt><dd>My First Definition</dd>\n<dd>My Second Definition</dd></dl>');
|
||||
expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('<dl><div><dt>My term</dt><dd>My First Definition</dd>\n<dd>My Second Definition</dd></div></dl>');
|
||||
});
|
||||
|
||||
test('Three Definitions', function() {
|
||||
const source = 'My term :: My First Definition :: My Second Definition :: My Third Definition';
|
||||
const source = 'My term :: My First Definition :: My Second Definition :: My Third Definition\n\n';
|
||||
const rendered = Markdown.render(source);
|
||||
expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('<dl><dt>My term</dt><dd>My First Definition</dd>\n<dd>My Second Definition</dd>\n<dd>My Third Definition</dd></dl>');
|
||||
expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('<dl><div><dt>My term</dt><dd>My First Definition</dd>\n<dd>My Second Definition</dd>\n<dd>My Third Definition</dd></div></dl>');
|
||||
});
|
||||
|
||||
test('Multiline Definitions', function() {
|
||||
const source = '**Example** :: V3 uses HTML *definition lists* to create "lists" with hanging indents.\n::Three\n::Four\n\nHello::I\'m a different\n::List\n\n';
|
||||
const rendered = Markdown.render(source);
|
||||
expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('<dl><div><dt><strong>Example</strong></dt><dd>V3 uses HTML <em>definition lists</em> to create “lists” with hanging indents.</dd>\n<dd>Three</dd>\n<dd>Four</dd></div></dl><dl><div><dt>Hello</dt><dd>I\’m a different</dd>\n<dd>List</dd></div></dl>');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@@ -968,7 +968,7 @@ body { counter-reset : phb-page-numbers; }
|
||||
& + * { margin-top : 0.17cm; }
|
||||
}
|
||||
p + dl { margin-top : 0.17cm; }
|
||||
dt {
|
||||
dt {
|
||||
display : inline;
|
||||
margin-right : 5px;
|
||||
margin-left : -1em;
|
||||
|
||||
Reference in New Issue
Block a user