Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/expandableImages.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,20 @@ module.exports = function expandableImages() {
link.alt &&
link.alt.endsWith("expandable")
) {
const altText = link.alt.replace(/expandable$/, "").trim()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, although this implementation is definitely easier to read than what we had before, the original implementation is approximately 50 times faster than this one (according to a quick https://jsbench.me/ test).

Because this is a low-level utility which is distributed without transpilation of any kind, little optimizations like this can end up mattering a lot more than they would in our primary codebase; I think we want to preserve the original. Might also be worth adding a comment explicitly calling out why we're prioritizing performance over readability here!

link.type = "span";
link.data = {
hName: "span",
hProperties: {
dataUrl: link.url,
dataAlt: altText,
className: "expandable-image"
}
};
link.children = [
{
type: "text",
value: link.alt.substr(0, -1 * "expandable".length).trim()
value: altText
}
];
}
Expand Down
54 changes: 54 additions & 0 deletions test/expandableImages.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const test = require("tape")
const { markdownToHtml } = require("./utils")
const expandableImages = require("../src/expandableImages")

test("plain images are not converted to span with data attributes", (t) => {
t.plan(1)
const markdown = "![plain image](https://example.com/image.png)"
const expected = '<p><img src="https://example.com/image.png" alt="plain image"></p>'

const rendered = markdownToHtml(markdown, expandableImages)
t.equal(rendered, expected)
})

test("expandable images are converted to span with data attributes", (t) => {
t.plan(1)
const markdown = "Check out this cool ![expandable](https://example.com/image.png) dog!"
const expected =
'<p>Check out this cool <span data-url="https://example.com/image.png" data-alt="" class="expandable-image"></span> dog!</p>'

const rendered = markdownToHtml(markdown, expandableImages)
t.equal(rendered, expected)
})

test("expandable image with alt text are converted to span with data attributes", (t) => {
t.plan(1)
const markdown =
"Check out this cool ![image of a dog expandable](https://example.com/image.png) dog!"
const expected =
'<p>Check out this cool <span data-url="https://example.com/image.png" data-alt="image of a dog" class="expandable-image">image of a dog</span> dog!</p>'

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 = "![expandable image](https://example.com/image.png)"
const expected =
'<p><img src="https://example.com/image.png" alt="expandable image"></p>'

const rendered = markdownToHtml(markdown, expandableImages)
t.equal(rendered, expected)
})

test("'expandable' flag at the end of the alt text handles 'expandable' elsewhere in the alt text", (t) => {
t.plan(1)
const markdown =
"Check out this cool ![expandable image of a dog expandable](https://example.com/image.png) dog!"
const expected =
'<p>Check out this cool <span data-url="https://example.com/image.png" data-alt="expandable image of a dog" class="expandable-image">expandable image of a dog</span> dog!</p>'

const rendered = markdownToHtml(markdown, expandableImages)
t.equal(rendered, expected)
})
2 changes: 1 addition & 1 deletion test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ schema.attributes.img.push('height', 'width');

// Add support for expandableImages
schema.tagNames.push('span');
schema.attributes.span = ['dataUrl', 'className'];
schema.attributes.span = ['dataUrl', 'dataAlt', 'className'];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you'll also want to apply this change here over in the main repo PR


// Add support for inline styles (gross)
// TODO replace all inline styles in our curriculum content with
Expand Down
Loading