Skip to content
This repository was archived by the owner on Feb 28, 2022. It is now read-only.

Commit dbcf627

Browse files
authored
Merge pull request #368 from /issues/338
feat(html pipe): Unwrap hero images
2 parents dabab11 + 921e737 commit dbcf627

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

src/defaults/html.pipe.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const validate = require('../utils/validate');
3131
const { cache, uncached } = require('../html/shared-cache');
3232
const embeds = require('../html/find-embeds');
3333
const parseFrontmatter = require('../html/parse-frontmatter');
34+
const unwrapSoleImages = require('../html/unwrap-sole-images');
3435
const rewriteLinks = require('../html/static-asset-links');
3536
const tovdom = require('../html/html-to-vdom');
3637
const tohtml = require('../html/stringify-response');
@@ -64,6 +65,7 @@ const htmlpipe = (cont, context, action) => {
6465
.before(smartypants)
6566
.before(sections)
6667
.before(meta).expose('meta')
68+
.before(unwrapSoleImages)
6769
.before(selectstrain)
6870
.before(selecttest)
6971
.before(html).expose('html')

src/html/unwrap-sole-images.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2019 Adobe. All rights reserved.
3+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License. You may obtain a copy
5+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under
8+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9+
* OF ANY KIND, either express or implied. See the License for the specific language
10+
* governing permissions and limitations under the License.
11+
*/
12+
const map = require('unist-util-map');
13+
14+
/**
15+
* Unwraps hero images to avoid the unnecessary paragraph.
16+
*
17+
* @param {object} request The content request
18+
*/
19+
function unwrap({ content }) {
20+
content.sections.forEach((section) => {
21+
map(section, (node, index, parent) => {
22+
if (node.type === 'paragraph' // If we have a paragraph
23+
&& parent.type === 'root' // … in a top section
24+
&& parent.types.includes('has-only-image') // … that only has images
25+
&& parent.types.includes('nb-image-1')) { // … and actually only 1 of them
26+
// … then consider it a hero image, and unwrap from the paragraph
27+
const position = content.mdast.children.indexOf(node);
28+
const [img] = content.mdast.children[position].children;
29+
content.mdast.children[position] = img;
30+
}
31+
});
32+
});
33+
}
34+
35+
module.exports = unwrap;

test/testHTMLFromMarkdown.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,4 +363,29 @@ describe('Testing Markdown conversion', () => {
363363
SANITIZE_DOM: true,
364364
});
365365
});
366+
367+
it('Unwraps hero images', async () => {
368+
await assertMd(`
369+
![Foo](/bar.png)
370+
`, `
371+
<img sizes="100vw" srcset="/bar.png?width=480&amp;auto=webp 480w,/bar.png?width=1384&amp;auto=webp 1384w,/bar.png?width=2288&amp;auto=webp 2288w,/bar.png?width=3192&amp;auto=webp 3192w,/bar.png?width=4096&amp;auto=webp 4096w" src="/bar.png" alt="Foo"/>
372+
`, {
373+
SANITIZE_DOM: true,
374+
});
375+
});
376+
377+
it('Leaves regular images inside paragraphs', async () => {
378+
await assertMd(`
379+
# Baz
380+
381+
![Foo](/bar.png)
382+
`, `
383+
<h1 id="baz">Baz</h1>
384+
<p>
385+
<img sizes="100vw" srcset="/bar.png?width=480&amp;auto=webp 480w,/bar.png?width=1384&amp;auto=webp 1384w,/bar.png?width=2288&amp;auto=webp 2288w,/bar.png?width=3192&amp;auto=webp 3192w,/bar.png?width=4096&amp;auto=webp 4096w" src="/bar.png" alt="Foo"/>
386+
</p>
387+
`, {
388+
SANITIZE_DOM: true,
389+
});
390+
});
366391
});

0 commit comments

Comments
 (0)