diff --git a/package.json b/package.json index e2d0e33..25abb96 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@code-dot-org/remark-plugins", - "version": "2.0.0", + "version": "2.0.1", "description": "Library of all Remark plugins we use", "main": "src/index.js", "files": [ diff --git a/src/expandableImages.js b/src/expandableImages.js index 68c417c..d528dd1 100644 --- a/src/expandableImages.js +++ b/src/expandableImages.js @@ -30,7 +30,8 @@ module.exports = function expandableImages() { link.children = [ { type: "text", - value: link.alt.substr(0, -1 * "expandable".length).trim() + // using slice instead of regular expression for speed! + value: link.alt.slice(0, -1 * "expandable".length).trim() } ]; } diff --git a/test/expandableImages.test.js b/test/expandableImages.test.js new file mode 100644 index 0000000..e1dfe39 --- /dev/null +++ b/test/expandableImages.test.js @@ -0,0 +1,54 @@ +const test = require("tape") +const { markdownToHtml } = require("./utils") +const expandableImages = require("../src/expandableImages") + +test("plain image markdown is converted to a regular img element", (t) => { + t.plan(1) + const markdown = "" + const expected = '

Check out this cool dog!
' + + const rendered = markdownToHtml(markdown, expandableImages) + t.equal(rendered, expected) +}) + +test("expandable image markdown with alt text is converted to a span with a data-url attribute and alt text content", (t) => { + t.plan(1) + const markdown = + "Check out this cool  dog!" + const expected = + 'Check out this cool dog!
' + + const rendered = markdownToHtml(markdown, expandableImages) + t.equal(rendered, expected) +}) + +test("'expandable' anywhere but the end of the alt text does not make it expandable", (t) => { + t.plan(1) + const markdown = "" + const expected = + '
Check out this cool dog!
' + + const rendered = markdownToHtml(markdown, expandableImages) + t.equal(rendered, expected) +})