From 769f636db2b13606d1a411ba45ebc5b480552e4b Mon Sep 17 00:00:00 2001 From: David Bolack Date: Wed, 6 Dec 2023 17:48:51 -0600 Subject: [PATCH] Small fix and test updates Discovered that classes ( and possibly other splits could end up with an empty/null member that still gets joined so I added a trim to the end of all the joins in processStyleTags. Added tests that SHOULD test for bloc-level and inline-span moustaches with added attributes ( a=b ) --- shared/naturalcrit/markdown.js | 7 ++++--- tests/markdown/mustache-syntax.test.js | 12 ++++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/shared/naturalcrit/markdown.js b/shared/naturalcrit/markdown.js index 592de4896..fdc1b06ce 100644 --- a/shared/naturalcrit/markdown.js +++ b/shared/naturalcrit/markdown.js @@ -359,6 +359,7 @@ const processStyleTags = (string)=>{ const id = _.remove(tags, (tag)=>tag.startsWith('#')).map((tag)=>tag.slice(1))[0]; const classes = _.remove(tags, (tag)=>(!tag.includes(':')) && (!tag.includes('='))); + console.log(classes); let attributes = _.remove(tags, (tag)=>(!tag.includes(':')) && (!tag.includes('#'))); const styles = tags?.length ? tags.map((tag)=>tag.replace(/:"?([^"]*)"?/g, ':$1;').trim()) : []; @@ -366,10 +367,10 @@ const processStyleTags = (string)=>{ attributes = attributes.map((attr)=>attr.replace(/="?([^"]*)"?/g, '="$1"')); } - return `${classes.join(' ')}" ` + + return `${classes.join(' ').trim()}" ` + `${id ? `id="${id}"` : ''} ` + - `${styles?.length ? `style="${styles.join(' ')}"` : ''}` + - `${attributes?.length ? ` ${attributes.join(' ')}` : ''}`; + `${styles?.length ? `style="${styles.join(' ').trim()}"` : ''}` + + `${attributes?.length ? ` ${attributes.join(' ').trim()}` : ''}`; }; module.exports = { diff --git a/tests/markdown/mustache-syntax.test.js b/tests/markdown/mustache-syntax.test.js index fc531cfdc..790af84f6 100644 --- a/tests/markdown/mustache-syntax.test.js +++ b/tests/markdown/mustache-syntax.test.js @@ -130,6 +130,18 @@ describe('Inline: When using the Inline syntax {{ }}', ()=>{ const rendered = Markdown.render(source).trimReturns(); expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe(`

homebrew mug

`); }); + + it('Render a span with added attributes', function() { + const source = 'Text and {{pen,#author,color:orange,font-family:"trebuchet ms",a="b and c",d=e, text}} and more text!'; + const rendered = Markdown.render(source); + expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('

Text and text and more text!

\n'); + }); + + it('Render a div with added attributes', function() { + const source = '{{pen,#author,color:orange,font-family:"trebuchet ms",a="b and c",d=e\nText and text and more text!\n}}\n'; + const rendered = Markdown.render(source); + expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('

Text and text and more text!

\n
'); + }); }); // BLOCK SYNTAX