-
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 vimeo embed * Update readme and changelog * Remove extra whitespace * Update copyright, fix twitter link, extend test cases * Remove support for invalid URL --------- Co-authored-by: szabi <[email protected]>
- Loading branch information
Showing
9 changed files
with
272 additions
and
1 deletion.
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,108 @@ | ||
/* | ||
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/vimeo | ||
*/ | ||
|
||
const reduceFraction = require('../../util/reduce_fraction'); | ||
|
||
/** | ||
* Add support for [Vimeo](http://player.vimeo.com) embeds in Markdown, as block syntax. | ||
* | ||
* The basic syntax is `[vimeo <url>]`. E.g., `[vimeo https://player.vimeo.com/video/329272793]`. | ||
* Height and width can optionally be set using `[vimeo <url> [height] [width]]`. E.g., `[vimeo https://player.vimeo.com/video/329272793 380 560]`. | ||
* The default value for height is 270 and for width is 480. | ||
* | ||
* @example | ||
* [vimeo https://player.vimeo.com/video/329272793] | ||
* | ||
* <iframe src="https://player.vimeo.com/video/329272793" class="vimeo" height="270" width="480" style="aspect-ratio: 16/9" frameborder="0" allowfullscreen> | ||
* <a href="https://player.vimeo.com/video/329272793" target="_blank">View Vimeo video</a> | ||
* </iframe> | ||
* | ||
* @type {import('markdown-it').PluginSimple} | ||
*/ | ||
module.exports = md => { | ||
/** | ||
* Parsing rule for Vimeo markup. | ||
* | ||
* @type {import('markdown-it/lib/parser_block').RuleBlock} | ||
* @private | ||
*/ | ||
const vimeoRule = (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 < 9) return false; // [vimeo a] | ||
if (currentLine.slice(0, 7) !== '[vimeo ') return false; | ||
if (currentLine[currentLine.length - 1] !== ']') return false; | ||
|
||
// Check for vimeo match | ||
const match = currentLine.match(/^\[vimeo (?:(?:(?:https?:)?\/\/)?player\.vimeo\.com\/video\/)?(\d+)?(?: (\d+))?(?: (\d+))?]$/); | ||
if (!match) return false; | ||
|
||
// Get the id from the url | ||
const id = match[1]; | ||
if (!id) return false; | ||
|
||
// Get the height | ||
const height = Number(match[2]) || 270; | ||
|
||
// Get the width | ||
const width = Number(match[3]) || 480; | ||
|
||
// Update the pos for the parser | ||
state.line = startLine + 1; | ||
|
||
// Add token to state | ||
const token = state.push('vimeo', 'vimeo', 0); | ||
token.block = true; | ||
token.markup = match[0]; | ||
token.vimeo = { id: Number(id), height, width }; | ||
|
||
// Done | ||
return true; | ||
}; | ||
|
||
md.block.ruler.before('paragraph', 'vimeo', vimeoRule); | ||
|
||
/** | ||
* Rendering rule for Vimeo markup. | ||
* | ||
* @type {import('markdown-it/lib/renderer').RenderRule} | ||
* @private | ||
*/ | ||
md.renderer.rules.vimeo = (tokens, index) => { | ||
const token = tokens[index]; | ||
|
||
// Determine the aspect ratio | ||
const aspectRatio = reduceFraction(token.vimeo.width, token.vimeo.height).join('/'); | ||
|
||
// Return the HTML | ||
return `<iframe src="https://player.vimeo.com/video/${encodeURIComponent(token.vimeo.id)}" class="vimeo" height="${token.vimeo.height}" width="${token.vimeo.width}" style="aspect-ratio: ${aspectRatio}" frameborder="0" allowfullscreen> | ||
<a href="https://player.vimeo.com/video/${encodeURIComponent(token.vimeo.id)}" target="_blank">View Vimeo video</a> | ||
</iframe>\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,88 @@ | ||
/* | ||
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('./vimeo')); | ||
|
||
it('handles vimeo embeds (not inline)', () => { | ||
expect(md.render('[vimeo https://player.vimeo.com/video/329272793 280 560]')).toBe(`<iframe src="https://player.vimeo.com/video/329272793" class="vimeo" height="280" width="560" style="aspect-ratio: 2/1" frameborder="0" allowfullscreen> | ||
<a href="https://player.vimeo.com/video/329272793" target="_blank">View Vimeo video</a> | ||
</iframe> | ||
`); | ||
}); | ||
|
||
it('handles vimeo embeds with no id (no embed)', () => { | ||
expect(md.render('[vimeo ]')).toBe(`<p>[vimeo ]</p> | ||
`); | ||
}); | ||
|
||
it('handles vimeo embeds that are unclosed (no embed)', () => { | ||
expect(md.render('[vimeo https://player.vimeo.com/video/329272793')).toBe(`<p>[vimeo https://player.vimeo.com/video/329272793</p> | ||
`); | ||
}); | ||
|
||
it('handles vimeo embeds with http', () => { | ||
expect(md.render('[vimeo http://player.vimeo.com/video/329272793]')).toBe(`<iframe src="https://player.vimeo.com/video/329272793" class="vimeo" height="270" width="480" style="aspect-ratio: 16/9" frameborder="0" allowfullscreen> | ||
<a href="https://player.vimeo.com/video/329272793" target="_blank">View Vimeo video</a> | ||
</iframe> | ||
`); | ||
}); | ||
|
||
it('handles vimeo embeds with no https:', () => { | ||
expect(md.render('[vimeo //player.vimeo.com/video/329272793]')).toBe(`<iframe src="https://player.vimeo.com/video/329272793" class="vimeo" height="270" width="480" style="aspect-ratio: 16/9" frameborder="0" allowfullscreen> | ||
<a href="https://player.vimeo.com/video/329272793" target="_blank">View Vimeo video</a> | ||
</iframe> | ||
`); | ||
}); | ||
|
||
it('handles vimeo embeds with no https://', () => { | ||
expect(md.render('[vimeo player.vimeo.com/video/329272793]')).toBe(`<iframe src="https://player.vimeo.com/video/329272793" class="vimeo" height="270" width="480" style="aspect-ratio: 16/9" frameborder="0" allowfullscreen> | ||
<a href="https://player.vimeo.com/video/329272793" target="_blank">View Vimeo video</a> | ||
</iframe> | ||
`); | ||
}); | ||
|
||
it('handles vimeo embeds with just the video ID', () => { | ||
expect(md.render('[vimeo 329272793]')).toBe(`<iframe src="https://player.vimeo.com/video/329272793" class="vimeo" height="270" width="480" style="aspect-ratio: 16/9" frameborder="0" allowfullscreen> | ||
<a href="https://player.vimeo.com/video/329272793" target="_blank">View Vimeo video</a> | ||
</iframe> | ||
`); | ||
}); | ||
|
||
it('handles vimeo embeds without width', () => { | ||
expect(md.render('[vimeo https://player.vimeo.com/video/329272793 240]')).toBe(`<iframe src="https://player.vimeo.com/video/329272793" class="vimeo" height="240" width="480" style="aspect-ratio: 2/1" frameborder="0" allowfullscreen> | ||
<a href="https://player.vimeo.com/video/329272793" target="_blank">View Vimeo video</a> | ||
</iframe> | ||
`); | ||
}); | ||
|
||
it('handles vimeo embeds without width or height', () => { | ||
expect(md.render('[vimeo https://player.vimeo.com/video/329272793]')).toBe(`<iframe src="https://player.vimeo.com/video/329272793" class="vimeo" height="270" width="480" style="aspect-ratio: 16/9" frameborder="0" allowfullscreen> | ||
<a href="https://player.vimeo.com/video/329272793" target="_blank">View Vimeo video</a> | ||
</iframe> | ||
`); | ||
}); | ||
|
||
it('handles vimeo embeds attempting html injection', () => { | ||
expect(md.render('[vimeo <script>alert();</script> 280 560]')).toBe(`<p>[vimeo <script>alert();</script> 280 560]</p> | ||
`); | ||
}); | ||
|
||
it('handles vimeo embeds attempting url manipulation', () => { | ||
expect(md.render('[vimeo a/../../b 280 560]')).toBe(`<p>[vimeo a/../../b 280 560]</p> | ||
`); | ||
}); |
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,23 @@ | ||
/* | ||
Copyright 2023 DigitalOcean | ||
Licensed under the Apache License, Version 2.0 (the "License") !default; | ||
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. | ||
*/ | ||
|
||
// Vimeo embeds | ||
.vimeo { | ||
display: block; | ||
height: auto; | ||
margin: 1em auto; | ||
max-width: 100%; | ||
} |
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