mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-14 04:12:43 +00:00
Add PANdoc style lists ( using :: not : )
Includes new tests and fixes broken old tests
This commit is contained in:
@@ -239,7 +239,7 @@ const definitionLists = {
|
|||||||
level : 'block',
|
level : 'block',
|
||||||
start(src) { return src.match(/^.*?::.*\n\n/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) {
|
tokenizer(src, tokens) {
|
||||||
const regex = /^([^\n:]*?)::(.*)(?:\n|$)/ym;
|
const regex = /^([^:\n]*?)[\n]?::(.*)(?:\n|$)/ym;
|
||||||
let match;
|
let match;
|
||||||
const endIndex = src.match(`\n\n`)?.index + 2;
|
const endIndex = src.match(`\n\n`)?.index + 2;
|
||||||
const allDefinitions = [];
|
const allDefinitions = [];
|
||||||
@@ -258,7 +258,6 @@ const definitionLists = {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const newDefinitions = match[2].split('::').filter((item)=>item).map((s)=>this.lexer.inlineTokens(s.trim()));
|
const newDefinitions = match[2].split('::').filter((item)=>item).map((s)=>this.lexer.inlineTokens(s.trim()));
|
||||||
console.log(newDefinitions);
|
|
||||||
if(newDefinitions?.length) {
|
if(newDefinitions?.length) {
|
||||||
currentDefinition.dd.push(newDefinitions);
|
currentDefinition.dd.push(newDefinitions);
|
||||||
} else {
|
} else {
|
||||||
@@ -277,11 +276,13 @@ const definitionLists = {
|
|||||||
renderer(token) {
|
renderer(token) {
|
||||||
let returnVal = `<dl>`;
|
let returnVal = `<dl>`;
|
||||||
token.definitions.forEach((def)=>{
|
token.definitions.forEach((def)=>{
|
||||||
const dds = def.dd.map((ddef)=>{
|
let dds = def.dd.map((ddef)=>{
|
||||||
return ddef.map((s)=>`<dd>${this.parser.parseInline(s)}</dd>`).join('');
|
return ddef.map((s)=>`<dd>${this.parser.parseInline(s)}</dd>`).join('');
|
||||||
}).join('\n');
|
}).join('\n');
|
||||||
|
dds = dds.trim();
|
||||||
returnVal += `<dt>${this.parser.parseInline(def.dt)}</dt>${dds.indexOf('\n') > -1 ? '\n' : ''}${dds}\n`;
|
returnVal += `<dt>${this.parser.parseInline(def.dt)}</dt>${dds.indexOf('\n') > -1 ? '\n' : ''}${dds}\n`;
|
||||||
});
|
});
|
||||||
|
returnVal = returnVal.trim();
|
||||||
return `${returnVal}</dl>`;
|
return `${returnVal}</dl>`;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -6,49 +6,74 @@ describe('Dictionary Terms', ()=>{
|
|||||||
test('Single Definition', function() {
|
test('Single Definition', function() {
|
||||||
const source = 'My term :: My First Definition\n\n';
|
const source = 'My term :: My First Definition\n\n';
|
||||||
const rendered = Markdown.render(source);
|
const rendered = Markdown.render(source);
|
||||||
expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('<dl><dt>My term</dt><dd>My First Definition</dd>\n</dl>');
|
expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('<dl><dt>My term</dt><dd>My First Definition</dd></dl>');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Two Definitions', function() {
|
test('Two Definitions', function() {
|
||||||
const source = 'My term :: My First Definition :: My Second Definition\n\n';
|
const source = 'My term :: My First Definition :: My Second Definition\n\n';
|
||||||
const rendered = Markdown.render(source);
|
const rendered = Markdown.render(source);
|
||||||
expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('<dl><dt>My term</dt><dd>My First Definition</dd><dd>My Second Definition</dd>\n</dl>');
|
expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('<dl><dt>My term</dt><dd>My First Definition</dd><dd>My Second Definition</dd></dl>');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Three Definitions', function() {
|
test('Three Definitions', function() {
|
||||||
const source = 'My term :: My First Definition :: My Second Definition :: My Third Definition\n\n';
|
const source = 'My term :: My First Definition :: My Second Definition :: My Third Definition\n\n';
|
||||||
const rendered = Markdown.render(source);
|
const rendered = Markdown.render(source);
|
||||||
expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('<dl><dt>My term</dt><dd>My First Definition</dd><dd>My Second Definition</dd><dd>My Third Definition</dd>\n</dl>');
|
expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('<dl><dt>My term</dt><dd>My First Definition</dd><dd>My Second Definition</dd><dd>My Third Definition</dd></dl>');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Multiline Definitions', function() {
|
test('Multiline Definitions', function() {
|
||||||
const source = '**Example** :: V3 uses HTML *definition lists* to create "lists" with hanging indents.\n::Two::\nThree\n::Four\n\n';
|
const source = '**Example** :: V3 uses HTML *definition lists* to create "lists" with hanging indents.\n::Two\n::Three\n::Four\n\n';
|
||||||
const rendered = Markdown.render(source);
|
const rendered = Markdown.render(source);
|
||||||
expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('<dl><dt><strong>Example</strong></dt>\n<dd>V3 uses HTML <em>definition lists</em> to create “lists” with hanging indents.</dd>\n<dd>Two</dd\n<dd>Three</dd>\n<dd>Four</dd>\n</dl><dl>');
|
expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('<dl><dt><strong>Example</strong></dt>\n<dd>V3 uses HTML <em>definition lists</em> to create “lists” with hanging indents.</dd>\n<dd>Two</dd>\n<dd>Three</dd>\n<dd>Four</dd></dl>');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Multiple Definition Terms, single line, single definition', function() {
|
test('Multiple Definition Terms, single line, single definition', function() {
|
||||||
const source = 'Term 1::Definition of Term 1\nTerm 2::Definition of Term 2';
|
const source = 'Term 1::Definition of Term 1\nTerm 2::Definition of Term 2\n\n';
|
||||||
const rendered = Markdown.render(source);
|
const rendered = Markdown.render(source);
|
||||||
expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('<dl><dt>Term 1</dt><dd>Definition of Term 1</dd><dt>Term 2></dt><dd>Definition of Term 2</dd>\n</dl>');
|
expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('<dl><dt>Term 1</dt><dd>Definition of Term 1</dd>\n<dt>Term 2</dt><dd>Definition of Term 2</dd></dl>');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Multiple Definition Terms, single line, multiple definitions', function() {
|
test('Multiple Definition Terms, single line, multiple definitions', function() {
|
||||||
const source = 'Term 1::Definition 1 of Term 1::Definition 2 of Term 1\nTerm 2::Definition 1 of Term 2::Definition 2 of Term 2';
|
const source = 'Term 1::Definition 1 of Term 1::Definition 2 of Term 1\nTerm 2::Definition 1 of Term 2::Definition 2 of Term 2\n\n';
|
||||||
const rendered = Markdown.render(source);
|
const rendered = Markdown.render(source);
|
||||||
expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('<dl><dt>Term 1</dt><dd>Definition of Term 1</dd><dt>Term 2></dt><dd>Definition of Term 2</dd>\n</dl>');
|
expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('<dl><dt>Term 1</dt><dd>Definition 1 of Term 1</dd><dd>Definition 2 of Term 1</dd>\n<dt>Term 2</dt><dd>Definition 1 of Term 2</dd><dd>Definition 2 of Term 2</dd></dl>');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Multiple Definition Terms, single definitions, multiple lines', function() {
|
test('Multiple Definition Terms, single definitions, multiple lines', function() {
|
||||||
const source = 'Term 1::Definition 1 of Term 1\n::Definition 2 of Term 1\nTerm 2::Definition of Term 2';
|
const source = 'Term 1::Definition 1 of Term 1\n::Definition 2 of Term 1\nTerm 2::Definition of Term 2\n\n';
|
||||||
const rendered = Markdown.render(source);
|
const rendered = Markdown.render(source);
|
||||||
expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('<dl><dt>Term 1</dt><dd>Definition of Term 1</dd><dt>Term 2></dt><dd>Definition of Term 2</dd>\n</dl>');
|
expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('<dl><dt>Term 1</dt>\n<dd>Definition 1 of Term 1</dd>\n<dd>Definition 2 of Term 1</dd>\n<dt>Term 2</dt><dd>Definition of Term 2</dd></dl>');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Multiple Definition Terms, multiple mixed-line definitions', function() {
|
test('Multiple Definition Terms, multiple mixed-line definitions', function() {
|
||||||
const source = 'Term 1::Definition 1 of Term 1\n::Definition 2 of Term 1::Definition 3 of Term 1\nTerm 2::Definition of Term 2';
|
const source = 'Term 1::Definition 1 of Term 1\n::Definition 2 of Term 1::Definition 3 of Term 1\nTerm 2::Definition of Term 2\n\n';
|
||||||
const rendered = Markdown.render(source);
|
const rendered = Markdown.render(source);
|
||||||
expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('<dl><dt>Term 1</dt><dd>Definition of Term 1</dd><dt>Term 2></dt><dd>Definition of Term 2</dd>\n</dl>');
|
expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('<dl><dt>Term 1</dt>\n<dd>Definition 1 of Term 1</dd>\n<dd>Definition 2 of Term 1</dd><dd>Definition 3 of Term 1</dd>\n<dt>Term 2</dt><dd>Definition of Term 2</dd></dl>');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('PANdoc style list - Single Term, Single Definition', function() {
|
||||||
|
const source = 'Term 1\n::Definition 1\n\n';
|
||||||
|
const rendered = Markdown.render(source);
|
||||||
|
expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('<dl><dt>Term 1</dt><dd>Definition 1</dd></dl>');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('PANdoc style list - Single Term, Plural Definitions', function() {
|
||||||
|
const source = 'Term 1\n::Definition 1\n::Definition 2\n\n';
|
||||||
|
const rendered = Markdown.render(source);
|
||||||
|
expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('<dl><dt>Term 1</dt>\n<dd>Definition 1</dd>\n<dd>Definition 2</dd></dl>');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('PANdoc style list - Multiple Term, Single Definitions', function() {
|
||||||
|
const source = 'Term 1\n::Definition 1\nTerm 2\n::Definition 1\n\n';
|
||||||
|
const rendered = Markdown.render(source);
|
||||||
|
expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('<dl><dt>Term 1</dt><dd>Definition 1</dd>\n<dt>Term 2</dt><dd>Definition 1</dd></dl>');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('PANdoc style list - Multiple Term, Plural Definitions', function() {
|
||||||
|
const source = 'Term 1\n::Definition 1\n::Definition 2\nTerm 2\n::Definition 1\n::Definition 2\n\n';
|
||||||
|
const rendered = Markdown.render(source);
|
||||||
|
expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('<dl><dt>Term 1</dt>\n<dd>Definition 1</dd>\n<dd>Definition 2</dd>\n<dt>Term 2</dt>\n<dd>Definition 1</dd>\n<dd>Definition 2</dd></dl>');
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user