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

Commit b50613c

Browse files
committed
feat(html pipe): Unwrap hero images
Unwrap sole images in a single paragraph in a section so the hero image is not inside a paragraph tag fix #338
1 parent b422ee7 commit b50613c

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-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 unwrapHeroImages = require('../html/unwrap-hero-images.js');
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(unwrapHeroImages)
6769
.before(selectstrain)
6870
.before(selecttest)
6971
.before(html).expose('html')

src/html/unwrap-hero-images.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2018 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+
function reformat({ content }) {
15+
content.sections.forEach((section) => {
16+
map(section, (node, index, parent) => {
17+
if (node.type === 'paragraph' // If we have a paragraph
18+
&& parent.type === 'root' // … in a top section
19+
&& parent.types.includes('has-only-image') // … that only has images
20+
&& parent.types.includes('nb-image-1')) { // … and actually only 1 of them
21+
// … then consider it a hero image, and unwrap from the paragraph
22+
const position = content.mdast.children.indexOf(node);
23+
const [img] = content.mdast.children[position].children;
24+
content.mdast.children[position] = img;
25+
}
26+
});
27+
});
28+
}
29+
30+
module.exports = reformat;

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)