-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Image comparison plugin * Fix eslint problems * Changes based on review * Updates based on feedback * Remove '\' from output * Update default value docs --------- Co-authored-by: szabi <[email protected]>
- Loading branch information
Showing
9 changed files
with
351 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
Copyright 2023 DigitalOcean | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
/** | ||
* @module rules/embeds/compare | ||
*/ | ||
|
||
/** | ||
* Add support for Image Comparison in Markdown, as block syntax. | ||
* | ||
* The basic syntax is `[compare <url1> <url2>]`. E.g., `[compare https://assets.digitalocean.com/banners/python.png https://assets.digitalocean.com/banners/javascript.png]`. | ||
* Height and width can optionally be set using `[compare <url1> <url2> [height] [width]]`. E.g., `[compare https://assets.digitalocean.com/banners/python.png https://assets.digitalocean.com/banners/javascript.png 500 560]`. | ||
* The default value for height is 270 and for width is 480. | ||
* | ||
* @example | ||
* [compare https://assets.digitalocean.com/banners/python.png https://assets.digitalocean.com/banners/javascript.png] | ||
* | ||
* <div class="image-compare" style="--value:50%; height: 270px; width: 480px;"> | ||
* <img class="image-left" src="https://assets.digitalocean.com/banners/python.png" alt="Image left"/> | ||
* <img class="image-right" src="https://assets.digitalocean.com/banners/javascript.png" alt="Image right"/> | ||
* <input type="range" class="control" min="0" max="100" value="50" oninput="this.parentNode.style.setProperty('--value', `${this.value}%`)" /> | ||
* <svg class="control-arrow" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path fill="currentColor" d="M504.3 273.6c4.9-4.5 7.7-10.9 7.7-17.6s-2.8-13-7.7-17.6l-112-104c-7-6.5-17.2-8.2-25.9-4.4s-14.4 12.5-14.4 22l0 56-192 0 0-56c0-9.5-5.7-18.2-14.4-22s-18.9-2.1-25.9 4.4l-112 104C2.8 243 0 249.3 0 256s2.8 13 7.7 17.6l112 104c7 6.5 17.2 8.2 25.9 4.4s14.4-12.5 14.4-22l0-56 192 0 0 56c0 9.5 5.7 18.2 14.4 22s18.9 2.1 25.9-4.4l112-104z"/></svg> | ||
* </div> | ||
* | ||
* @type {import('markdown-it').PluginSimple} | ||
*/ | ||
module.exports = md => { | ||
/** | ||
* Parsing rule for Image compare markup. | ||
* | ||
* @type {import('markdown-it/lib/parser_block').RuleBlock} | ||
* @private | ||
*/ | ||
const compareRule = (state, startLine, endLine, silent) => { | ||
// If silent, don't replace | ||
if (silent) return false; | ||
|
||
// Get current string to consider (just current line) | ||
const pos = state.bMarks[startLine] + state.tShift[startLine]; | ||
const max = state.eMarks[startLine]; | ||
const currentLine = state.src.substring(pos, max); | ||
|
||
// Perform some non-regex checks for speed | ||
if (currentLine.length < 13) return false; // [compare a b] | ||
if (currentLine.slice(0, 9) !== '[compare ') return false; | ||
if (currentLine[currentLine.length - 1] !== ']') return false; | ||
|
||
// Check for compare match | ||
const match = currentLine.match(/^\[compare (\S+) (\S+)(?: (\d+))?(?: (\d+))?]$/); | ||
if (!match) return false; | ||
|
||
// Get the first image | ||
const imageLeft = match[1]; | ||
if (!imageLeft) return false; | ||
|
||
// Get the second image | ||
const imageRight = match[2]; | ||
if (!imageRight) return false; | ||
|
||
// Get the height | ||
const height = Number(match[3]) || 270; | ||
|
||
// Get the width | ||
const width = Number(match[4]) || 480; | ||
|
||
// Update the pos for the parser | ||
state.line = startLine + 1; | ||
|
||
// Add token to state | ||
const token = state.push('compare', 'compare', 0); | ||
token.block = true; | ||
token.markup = match[0]; | ||
token.compare = { imageLeft, imageRight, height, width }; | ||
|
||
// Done | ||
return true; | ||
}; | ||
|
||
md.block.ruler.before('paragraph', 'compare', compareRule); | ||
|
||
/** | ||
* Rendering rule for compare markup. | ||
* | ||
* @type {import('markdown-it/lib/renderer').RenderRule} | ||
* @private | ||
*/ | ||
md.renderer.rules.compare = (tokens, index) => { | ||
const token = tokens[index]; | ||
|
||
// Return the HTML | ||
return `<div class="image-compare" style="--value:50%; height: ${md.utils.escapeHtml(token.compare.height)}px; width: ${md.utils.escapeHtml(token.compare.width)}px;"> | ||
<img class="image-left" src="${md.utils.escapeHtml(token.compare.imageLeft)}" alt="Image left"/> | ||
<img class="image-right" src="${md.utils.escapeHtml(token.compare.imageRight)}" alt="Image right"/> | ||
<input type="range" class="control" min="0" max="100" value="50" oninput="this.parentNode.style.setProperty('--value', \`\${this.value}%\`)" /> | ||
<svg class="control-arrow" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path fill="currentColor" d="M504.3 273.6c4.9-4.5 7.7-10.9 7.7-17.6s-2.8-13-7.7-17.6l-112-104c-7-6.5-17.2-8.2-25.9-4.4s-14.4 12.5-14.4 22l0 56-192 0 0-56c0-9.5-5.7-18.2-14.4-22s-18.9-2.1-25.9 4.4l-112 104C2.8 243 0 249.3 0 256s2.8 13 7.7 17.6l112 104c7 6.5 17.2 8.2 25.9 4.4s14.4-12.5 14.4-22l0-56 192 0 0 56c0 9.5 5.7 18.2 14.4 22s18.9 2.1 25.9-4.4l112-104z"/></svg> | ||
</div>\n`; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
Copyright 2023 DigitalOcean | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const md = require('markdown-it')().use(require('./compare')); | ||
|
||
it('handles image compare embeds (not inline)', () => { | ||
expect(md.render('[compare https://assets.digitalocean.com/banners/python.png https://assets.digitalocean.com/banners/javascript.png 400 400]')).toBe(`<div class="image-compare" style="--value:50%; height: 400px; width: 400px;"> | ||
<img class="image-left" src="https://assets.digitalocean.com/banners/python.png" alt="Image left"/> | ||
<img class="image-right" src="https://assets.digitalocean.com/banners/javascript.png" alt="Image right"/> | ||
<input type="range" class="control" min="0" max="100" value="50" oninput="this.parentNode.style.setProperty('--value', \`\${this.value}%\`)" /> | ||
<svg class="control-arrow" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path fill="currentColor" d="M504.3 273.6c4.9-4.5 7.7-10.9 7.7-17.6s-2.8-13-7.7-17.6l-112-104c-7-6.5-17.2-8.2-25.9-4.4s-14.4 12.5-14.4 22l0 56-192 0 0-56c0-9.5-5.7-18.2-14.4-22s-18.9-2.1-25.9 4.4l-112 104C2.8 243 0 249.3 0 256s2.8 13 7.7 17.6l112 104c7 6.5 17.2 8.2 25.9 4.4s14.4-12.5 14.4-22l0-56 192 0 0 56c0 9.5 5.7 18.2 14.4 22s18.9 2.1 25.9-4.4l112-104z"/></svg> | ||
</div> | ||
`); | ||
}); | ||
|
||
it('handles image compare embeds with no urls (no embed)', () => { | ||
expect(md.render('[compare ]')).toBe(`<p>[compare ]</p> | ||
`); | ||
}); | ||
|
||
it('handles image compare embeds with one url (no embed)', () => { | ||
expect(md.render('[compare https://assets.digitalocean.com/banners/python.png]')).toBe(`<p>[compare https://assets.digitalocean.com/banners/python.png]</p> | ||
`); | ||
}); | ||
|
||
it('handles image compare embeds that are unclosed (no embed)', () => { | ||
expect(md.render('[compare https://assets.digitalocean.com/banners/python.png https://assets.digitalocean.com/banners/javascript.png')).toBe(`<p>[compare https://assets.digitalocean.com/banners/python.png https://assets.digitalocean.com/banners/javascript.png</p> | ||
`); | ||
}); | ||
|
||
it('handles image compare embeds without width', () => { | ||
expect(md.render('[compare https://assets.digitalocean.com/banners/python.png https://assets.digitalocean.com/banners/javascript.png 400]')).toBe(`<div class="image-compare" style="--value:50%; height: 400px; width: 480px;"> | ||
<img class="image-left" src="https://assets.digitalocean.com/banners/python.png" alt="Image left"/> | ||
<img class="image-right" src="https://assets.digitalocean.com/banners/javascript.png" alt="Image right"/> | ||
<input type="range" class="control" min="0" max="100" value="50" oninput="this.parentNode.style.setProperty('--value', \`\${this.value}%\`)" /> | ||
<svg class="control-arrow" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path fill="currentColor" d="M504.3 273.6c4.9-4.5 7.7-10.9 7.7-17.6s-2.8-13-7.7-17.6l-112-104c-7-6.5-17.2-8.2-25.9-4.4s-14.4 12.5-14.4 22l0 56-192 0 0-56c0-9.5-5.7-18.2-14.4-22s-18.9-2.1-25.9 4.4l-112 104C2.8 243 0 249.3 0 256s2.8 13 7.7 17.6l112 104c7 6.5 17.2 8.2 25.9 4.4s14.4-12.5 14.4-22l0-56 192 0 0 56c0 9.5 5.7 18.2 14.4 22s18.9 2.1 25.9-4.4l112-104z"/></svg> | ||
</div> | ||
`); | ||
}); | ||
|
||
it('handles image compare embeds without width or height', () => { | ||
expect(md.render('[compare https://assets.digitalocean.com/banners/python.png https://assets.digitalocean.com/banners/javascript.png]')).toBe(`<div class="image-compare" style="--value:50%; height: 270px; width: 480px;"> | ||
<img class="image-left" src="https://assets.digitalocean.com/banners/python.png" alt="Image left"/> | ||
<img class="image-right" src="https://assets.digitalocean.com/banners/javascript.png" alt="Image right"/> | ||
<input type="range" class="control" min="0" max="100" value="50" oninput="this.parentNode.style.setProperty('--value', \`\${this.value}%\`)" /> | ||
<svg class="control-arrow" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path fill="currentColor" d="M504.3 273.6c4.9-4.5 7.7-10.9 7.7-17.6s-2.8-13-7.7-17.6l-112-104c-7-6.5-17.2-8.2-25.9-4.4s-14.4 12.5-14.4 22l0 56-192 0 0-56c0-9.5-5.7-18.2-14.4-22s-18.9-2.1-25.9 4.4l-112 104C2.8 243 0 249.3 0 256s2.8 13 7.7 17.6l112 104c7 6.5 17.2 8.2 25.9 4.4s14.4-12.5 14.4-22l0-56 192 0 0 56c0 9.5 5.7 18.2 14.4 22s18.9 2.1 25.9-4.4l112-104z"/></svg> | ||
</div> | ||
`); | ||
}); | ||
|
||
it('handles image compare embeds attempting html injection', () => { | ||
expect(md.render('[compare <script>alert();</script>]')).toBe(`<p>[compare <script>alert();</script>]</p> | ||
`); | ||
}); | ||
|
||
it('handles image compare embeds attempting js injection', () => { | ||
expect(md.render('[compare " onload="alert();]')).toBe(`<div class="image-compare" style="--value:50%; height: 270px; width: 480px;"> | ||
<img class="image-left" src=""" alt="Image left"/> | ||
<img class="image-right" src="onload="alert();" alt="Image right"/> | ||
<input type="range" class="control" min="0" max="100" value="50" oninput="this.parentNode.style.setProperty('--value', \`\${this.value}%\`)" /> | ||
<svg class="control-arrow" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path fill="currentColor" d="M504.3 273.6c4.9-4.5 7.7-10.9 7.7-17.6s-2.8-13-7.7-17.6l-112-104c-7-6.5-17.2-8.2-25.9-4.4s-14.4 12.5-14.4 22l0 56-192 0 0-56c0-9.5-5.7-18.2-14.4-22s-18.9-2.1-25.9 4.4l-112 104C2.8 243 0 249.3 0 256s2.8 13 7.7 17.6l112 104c7 6.5 17.2 8.2 25.9 4.4s14.4-12.5 14.4-22l0-56 192 0 0 56c0 9.5 5.7 18.2 14.4 22s18.9 2.1 25.9-4.4l112-104z"/></svg> | ||
</div> | ||
`); | ||
}); | ||
|
||
it('handles compare embeds attempting url manipulation', () => { | ||
expect(md.render('[compare a/../../b a/../../b 280 560]')).toBe(`<div class="image-compare" style="--value:50%; height: 280px; width: 560px;"> | ||
<img class="image-left" src="a/../../b" alt="Image left"/> | ||
<img class="image-right" src="a/../../b" alt="Image right"/> | ||
<input type="range" class="control" min="0" max="100" value="50" oninput="this.parentNode.style.setProperty('--value', \`\${this.value}%\`)" /> | ||
<svg class="control-arrow" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path fill="currentColor" d="M504.3 273.6c4.9-4.5 7.7-10.9 7.7-17.6s-2.8-13-7.7-17.6l-112-104c-7-6.5-17.2-8.2-25.9-4.4s-14.4 12.5-14.4 22l0 56-192 0 0-56c0-9.5-5.7-18.2-14.4-22s-18.9-2.1-25.9 4.4l-112 104C2.8 243 0 249.3 0 256s2.8 13 7.7 17.6l112 104c7 6.5 17.2 8.2 25.9 4.4s14.4-12.5 14.4-22l0-56 192 0 0 56c0 9.5 5.7 18.2 14.4 22s18.9 2.1 25.9-4.4l112-104z"/></svg> | ||
</div> | ||
`); | ||
}); |
Oops, something went wrong.