Skip to content

Commit

Permalink
[zmarkdown] Allow using custom element for iframes
Browse files Browse the repository at this point in the history
  • Loading branch information
StaloneLab committed Apr 26, 2024
1 parent 07983fa commit 7450014
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ exports[`HTML endpoint can enforce shifting level 1`] = `
<p>On a camera. What is happening with animals these days?\\"</p>"
`;
exports[`HTML endpoint can use custom iframe elements 1`] = `
"<h3 id=\\"you-wont-get-my-data-google\\">You won’t get my data, Google!<a aria-hidden=\\"true\\" tabindex=\\"-1\\" href=\\"#you-wont-get-my-data-google\\"><span class=\\"icon icon-link\\"></span></a></h3>
<hidden-frame src=\\"https://www.youtube.com/embed/q3zqJs7JUCQ?feature=oembed\\" width=\\"560\\" height=\\"315\\" allowfullscreen frameborder=\\"0\\"></hidden-frame>"
`;
exports[`HTML endpoint correctly renders manifest 1`] = `
Object {
"children": Array [
Expand Down
16 changes: 16 additions & 0 deletions packages/zmarkdown/__tests__/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,22 @@ describe('HTML endpoint', () => {
expect(content).toMatchSnapshot()
expect(content).toContain('h2')
})

it('can use custom iframe elements', async () => {
const text = dedent(`
# You won't get my data, Google!
!(https://www.youtube.com/watch?v=q3zqJs7JUCQ)
`)

const response = await a.post(html, {md: text, opts: {hide_iframes: true}})
expect(response.status).toBe(200)

const [content] = response.data
expect(content).not.toContain('iframe')
expect(content).toContain('hidden-frame')
expect(content).toMatchSnapshot()
})
})

describe('LaTeX endpoint', () => {
Expand Down
3 changes: 0 additions & 3 deletions packages/zmarkdown/config/mdast/iframes.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ module.exports = {
oembed: 'https://soundcloud.com/oembed'
},
'www.ina.fr': {
tag: 'iframe',
width: 620,
height: 349,
disabled: false,
Expand All @@ -54,7 +53,6 @@ module.exports = {
removeFileName: true
},
'www.jsfiddle.net': {
tag: 'iframe',
width: 560,
height: 560,
disabled: false,
Expand All @@ -69,7 +67,6 @@ module.exports = {
}
},
'jsfiddle.net': {
tag: 'iframe',
width: 560,
height: 560,
disabled: false,
Expand Down
3 changes: 2 additions & 1 deletion packages/zmarkdown/config/sanitize/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const katex = require('./katex')
const merge = require('deepmerge')

module.exports = merge.all([gh, katex, {
tagNames: ['span', 'abbr', 'figure', 'figcaption', 'iframe'],
tagNames: ['span', 'abbr', 'figure', 'figcaption', 'iframe', 'hidden-frame'],
attributes: {
a: ['ariaHidden', 'class', 'className'],
abbr: ['title'],
Expand All @@ -14,6 +14,7 @@ module.exports = merge.all([gh, katex, {
h2: ['ariaHidden'],
h3: ['ariaHidden'],
iframe: ['allowfullscreen', 'frameborder', 'height', 'src', 'width'],
'hidden-frame': ['allowfullscreen', 'frameborder', 'height', 'src', 'width'],
img: ['class', 'className'],
span: ['id', 'data-count', 'class', 'className'],
summary: ['class', 'className'],
Expand Down
7 changes: 7 additions & 0 deletions packages/zmarkdown/server/factories/config-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ module.exports = opts => {
}
}

// Allow using a custom element for iframes, initially for GDPR compliance
if (opts.hide_iframes) {
for (const providerName in mdastConfig.iframes) {
mdastConfig.iframes[providerName].tag = 'hidden-frame'
}
}

if (opts.inline === true) {
if (!Array.isArray(mdastConfig.disableTokenizers.block)) {
mdastConfig.disableTokenizers.block = []
Expand Down

0 comments on commit 7450014

Please sign in to comment.