0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-05 23:12:39 +00:00

Back to passing all tests (not the "failing" tests yet)

This commit is contained in:
Trevor Buckner
2024-05-03 12:12:55 -04:00
parent 9fb0f37718
commit 94905f8151
3 changed files with 49 additions and 37 deletions

View File

@@ -135,12 +135,12 @@ const mustacheSpans = {
}, },
renderer(token) { renderer(token) {
const tags = token.tags; const tags = token.tags;
tags.classes = ['inline-block', tags.classes].join(' '); tags.classes = ['inline-block', tags.classes].join(' ').trim();
return `<span` + return `<span` +
`${tags.id ? ` id="${tags.id}"` : ''}` +
`${tags.classes ? ` class="${tags.classes}"` : ''}` + `${tags.classes ? ` class="${tags.classes}"` : ''}` +
`${tags.id ? ` id="${tags.id}"` : ''}` +
`${tags.styles ? ` style="${tags.styles}"` : ''}` + `${tags.styles ? ` style="${tags.styles}"` : ''}` +
`${tags.attributes ? ` ${tags.attributes}` : ''}` + `${tags.attributes ? ` ${Object.entries(tags.attributes).map(([key, value]) => `${key}="${value}"`).join(' ')}` : ''}` +
`>${this.parser.parseInline(token.tokens)}</span>`; // parseInline to turn child tokens into HTML `>${this.parser.parseInline(token.tokens)}</span>`; // parseInline to turn child tokens into HTML
} }
}; };
@@ -191,13 +191,13 @@ const mustacheDivs = {
}, },
renderer(token) { renderer(token) {
const tags = token.tags; const tags = token.tags;
tags.classes = ['block', tags.classes].join(' '); tags.classes = ['block', tags.classes].join(' ').trim();
return `<div` + return `<div` +
`${tags.id ? ` id="${tags.id}"` : ''}` + `${tags.classes ? ` class="${tags.classes}"` : ''}` +
`${tags.classes ? ` class="${tags.classes}"` : ''}` + `${tags.id ? ` id="${tags.id}"` : ''}` +
`${tags.styles ? ` style="${tags.styles}"` : ''}` + `${tags.styles ? ` style="${tags.styles}"` : ''}` +
`${tags.attributes ? ` ${tags.attributes}` : ''}` + `${tags.attributes ? ` ${Object.entries(tags.attributes).map(([key, value]) => `${key}="${value}"`).join(' ')}` : ''}` +
`>${this.parser.parse(token.tokens)}</div>`; // parse to turn child tokens into HTML `>${this.parser.parse(token.tokens)}</div>`; // parse to turn child tokens into HTML
} }
}; };
@@ -230,18 +230,18 @@ const mustacheInjectInline = {
const originalTags = extractHTMLStyleTags(text); const originalTags = extractHTMLStyleTags(text);
const injectedTags = token.injectedTags; const injectedTags = token.injectedTags;
const tags = { const tags = {
id : originalTags.id || injectedTags.id, id : injectedTags.id || originalTags.id || null,
classes : [originalTags.classes, injectedTags.classes].join(' '), classes : [originalTags.classes, injectedTags.classes].join(' ').trim() || null,
styles : [originalTags.styles, injectedTags.styles].join(' '), styles : [originalTags.styles, injectedTags.styles].join(' ').trim() || null,
attributes : [originalTags.attributes, injectedTags.attributes].join(' ') attributes : Object.assign(originalTags.attributes ?? {}, injectedTags.attributes ?? {})
}; };
const openingTag = /(<[^\s<>]+)([^\n<>]*>.*)/s.exec(text); const openingTag = /(<[^\s<>]+)[^\n<>]*(>.*)/s.exec(text);
if(openingTag) { if(openingTag) {
return `${openingTag[1]}` + return `${openingTag[1]}` +
`${tags.id ? ` id="${tags.id}"` : ''}` +
`${tags.classes ? ` class="${tags.classes}"` : ''}` + `${tags.classes ? ` class="${tags.classes}"` : ''}` +
`${tags.id ? ` id="${tags.id}"` : ''}` +
`${tags.styles ? ` style="${tags.styles}"` : ''}` + `${tags.styles ? ` style="${tags.styles}"` : ''}` +
`${tags.attributes ? ` ${tags.attributes}` : ''}` + `${tags.attributes ? ` ${Object.entries(tags.attributes).map(([key, value]) => `${key}="${value}"`).join(' ')}` : ''}` +
`${openingTag[2]}`; // parse to turn child tokens into HTML `${openingTag[2]}`; // parse to turn child tokens into HTML
} }
return text; return text;
@@ -279,18 +279,18 @@ const mustacheInjectBlock = {
const originalTags = extractHTMLStyleTags(text); const originalTags = extractHTMLStyleTags(text);
const injectedTags = token.injectedTags; const injectedTags = token.injectedTags;
const tags = { const tags = {
id : originalTags.id || injectedTags.id, id : injectedTags.id || originalTags.id || null,
classes : [originalTags.classes, injectedTags.classes].join(' '), classes : [originalTags.classes, injectedTags.classes].join(' ').trim() || null,
styles : [originalTags.styles, injectedTags.styles].join(' '), styles : [originalTags.styles, injectedTags.styles].join(' ').trim() || null,
attributes : [originalTags.attributes, injectedTags.attributes].join(' ') attributes : Object.assign(originalTags.attributes ?? {}, injectedTags.attributes ?? {})
}; };
const openingTag = /(<[^\s<>]+)([^\n<>]*>.*)/s.exec(text); const openingTag = /(<[^\s<>]+)[^\n<>]*(>.*)/s.exec(text);
if(openingTag) { if(openingTag) {
return `${openingTag[1]}` + return `${openingTag[1]}` +
`${tags.id ? ` id="${tags.id}"` : ''}` +
`${tags.classes ? ` class="${tags.classes}"` : ''}` + `${tags.classes ? ` class="${tags.classes}"` : ''}` +
`${tags.id ? ` id="${tags.id}"` : ''}` +
`${tags.styles ? ` style="${tags.styles}"` : ''}` + `${tags.styles ? ` style="${tags.styles}"` : ''}` +
`${tags.attributes ? ` ${tags.attributes}` : ''}` + `${tags.attributes ? ` ${Object.entries(tags.attributes).map(([key, value]) => `${key}="${value}"`).join(' ')}` : ''}` +
`${openingTag[2]}`; // parse to turn child tokens into HTML `${openingTag[2]}`; // parse to turn child tokens into HTML
} }
return text; return text;
@@ -727,16 +727,23 @@ const processStyleTags = (string)=>{
//TODO: can we simplify to just split on commas? //TODO: can we simplify to just split on commas?
const tags = string.match(/(?:[^, ":=]+|[:=](?:"[^"]*"|))+/g); const tags = string.match(/(?:[^, ":=]+|[:=](?:"[^"]*"|))+/g);
const id = _.remove(tags, (tag)=>tag.startsWith('#')).map((tag)=>tag.slice(1))[0]; const id = _.remove(tags, (tag)=>tag.startsWith('#')).map((tag)=>tag.slice(1))[0] || null;
const classes = _.remove(tags, (tag)=>(!tag.includes(':')) && (!tag.includes('='))); const classes = _.remove(tags, (tag)=>(!tag.includes(':')) && (!tag.includes('='))).join(' ') || null;
const attributes = _.remove(tags, (tag)=>(tag.includes('='))).map((tag)=>tag.replace(/="?([^"]*)"?/g, '="$1"')); const attributes = _.remove(tags, (tag)=>(tag.includes('='))).map((tag)=>tag.replace(/="?([^"]*)"?/g, '="$1"'))
const styles = tags?.length ? tags.map((tag)=>tag.replace(/:"?([^"]*)"?/g, ':$1;').trim()) : []; ?.filter(attr => !attr.startsWith('class="') && !attr.startsWith('style="') && !attr.startsWith('id="'))
.reduce((obj, attr) => {
let [key, value] = attr.split("=");
value = value.replace(/"/g, '');
obj[key] = value;
return obj;
}, {}) || null;
const styles = tags?.length ? tags.map((tag)=>tag.replace(/:"?([^"]*)"?/g, ':$1;').trim()).join(' ') : null;
return { return {
id : id, id : id,
classes : classes.join(' '), classes : classes,
styles : styles.join(' '), styles : styles,
attributes : attributes.join(' ') attributes : _.isEmpty(attributes) ? null : attributes
}; };
}; };
@@ -746,13 +753,18 @@ const extractHTMLStyleTags = (htmlString)=> {
const styles = htmlString.match(/style="([^"]*)"/)?.[1] || null; const styles = htmlString.match(/style="([^"]*)"/)?.[1] || null;
const attributes = htmlString.match(/([a-z]+="[^"]*)"/g) const attributes = htmlString.match(/([a-z]+="[^"]*)"/g)
?.filter(attr => !attr.startsWith('class="') && !attr.startsWith('style="') && !attr.startsWith('id="')) ?.filter(attr => !attr.startsWith('class="') && !attr.startsWith('style="') && !attr.startsWith('id="'))
.join(' ') || null; .reduce((obj, attr) => {
let [key, value] = attr.split("=");
value = value.replace(/"/g, '');
obj[key] = value;
return obj;
}, {}) || null;
return { return {
id, id : id,
classes, classes : classes,
styles, styles : styles,
attributes attributes : _.isEmpty(attributes) ? null : attributes
}; };
}; };

View File

@@ -306,7 +306,7 @@ describe('Injection: When an injection tag follows an element', ()=>{
it('Renders an image with added attributes', function() { it('Renders an image with added attributes', function() {
const source = `![homebrew mug](https://i.imgur.com/hMna6G0.png) {position:absolute,bottom:20px,left:130px,width:220px,a="b and c",d=e}`; const source = `![homebrew mug](https://i.imgur.com/hMna6G0.png) {position:absolute,bottom:20px,left:130px,width:220px,a="b and c",d=e}`;
const rendered = Markdown.render(source).trimReturns(); const rendered = Markdown.render(source).trimReturns();
expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe(`<p><img class="" style="position:absolute; bottom:20px; left:130px; width:220px;" a="b and c" d="e" src="https://i.imgur.com/hMna6G0.png" alt="homebrew mug"></p>`); expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe(`<p><img style="position:absolute; bottom:20px; left:130px; width:220px;" src="https://i.imgur.com/hMna6G0.png" alt="homebrew mug" a="b and c" d="e"></p>`);
}); });
}); });

View File

@@ -329,7 +329,7 @@ describe('Normal Links and Images', ()=>{
const source = `![alt text](url){width:100px}`; const source = `![alt text](url){width:100px}`;
const rendered = Markdown.render(source).trimReturns(); const rendered = Markdown.render(source).trimReturns();
expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe(dedent` expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe(dedent`
<p><img class="" style="width:100px;" src="url" alt="alt text"></p>`.trimReturns()); <p><img style="width:100px;" src="url" alt="alt text"></p>`.trimReturns());
}); });
it('Renders normal links', function() { it('Renders normal links', function() {