|
1 | 1 | const visit = require(`unist-util-visit`)
|
2 | 2 | const cheerio = require(`cheerio`)
|
3 |
| -const Promise = require(`bluebird`) |
4 | 3 | const { oneLine } = require(`common-tags`)
|
5 | 4 | const _ = require(`lodash`)
|
6 | 5 |
|
7 |
| -const isPixelNumber = n => /\d+px$/.test(n) |
8 |
| - |
9 |
| -const isUnitlessNumber = n => { |
10 |
| - const nToNum = _.toNumber(n) |
11 |
| - return _.isFinite(nToNum) |
12 |
| -} |
| 6 | +const needsSemicolon = str => !str.endsWith(`;`) |
13 | 7 |
|
14 |
| -const isUnitlessOrPixelNumber = n => |
15 |
| - n && (isUnitlessNumber(n) || isPixelNumber(n)) |
| 8 | +/** |
| 9 | + * Convert anything to number, except for % value. |
| 10 | + * We don't have to check for other values (em, vw, etc.) |
| 11 | + * because the browsers will treat them as px anyway. |
| 12 | + * @param {*} n something to be converted to number |
| 13 | + * @returns {number} |
| 14 | + */ |
| 15 | +const convert = n => |
| 16 | + typeof n === `string` && n.trim().endsWith(`%`) ? NaN : parseInt(n, 10) |
16 | 17 |
|
17 |
| -const needsSemicolon = str => !str.endsWith(`;`) |
| 18 | +/** |
| 19 | + * Check whether all passed in arguments are valid number or not |
| 20 | + * @param {...number} args dimension to check |
| 21 | + * @returns {boolean} |
| 22 | + */ |
| 23 | +const isValidDimensions = (...args) => args.every(n => _.isFinite(n)) |
18 | 24 |
|
19 |
| -// Aspect ratio can only be determined if both width and height are unitless or |
20 |
| -// pixel values. Any other values mean the responsive wrapper is not applied. |
21 |
| -const acceptedDimensions = (width, height) => |
22 |
| - isUnitlessOrPixelNumber(width) && isUnitlessOrPixelNumber(height) |
| 25 | +module.exports = async ({ markdownAST }, pluginOptions = {}) => { |
| 26 | + const defaults = { |
| 27 | + wrapperStyle: ``, |
| 28 | + } |
| 29 | + const options = _.defaults(pluginOptions, defaults) |
| 30 | + visit(markdownAST, [`html`, `jsx`], node => { |
| 31 | + const $ = cheerio.load(node.value) |
| 32 | + const iframe = $(`iframe, object`) |
| 33 | + if (iframe.length === 0) { |
| 34 | + return |
| 35 | + } |
23 | 36 |
|
24 |
| -module.exports = ({ markdownAST }, pluginOptions = {}) => |
25 |
| - new Promise(resolve => { |
26 |
| - const defaults = { |
27 |
| - wrapperStyle: ``, |
| 37 | + const width = convert(iframe.attr(`width`)) |
| 38 | + const height = convert(iframe.attr(`height`)) |
| 39 | + if (!isValidDimensions(width, height)) { |
| 40 | + return |
28 | 41 | }
|
29 |
| - const options = _.defaults(pluginOptions, defaults) |
30 |
| - visit(markdownAST, [`html`, `jsx`], node => { |
31 |
| - const $ = cheerio.load(node.value) |
32 |
| - const iframe = $(`iframe, object`) |
33 |
| - if (iframe.length) { |
34 |
| - const width = iframe.attr(`width`) |
35 |
| - const height = iframe.attr(`height`) |
36 | 42 |
|
37 |
| - if (acceptedDimensions(width, height)) { |
38 |
| - const existingStyle = $(`iframe`).attr(`style`) // Other plugins might set border: 0 |
39 |
| - // so we make sure that we maintain those existing styles. If other styles like height or |
40 |
| - // width are already defined they will be overridden anyway. |
| 43 | + let fullStyle = $(`iframe`).attr(`style`) || `` // Other plugins might set border: 0 |
| 44 | + // so we make sure that we maintain those existing styles. If other styles like height or |
| 45 | + // width are already defined they will be overridden anyway. |
41 | 46 |
|
42 |
| - let fullStyle = `` |
43 |
| - if (existingStyle && needsSemicolon(existingStyle)) { |
44 |
| - fullStyle = `${existingStyle};` |
45 |
| - } else if (existingStyle) { |
46 |
| - fullStyle = existingStyle |
47 |
| - } |
| 47 | + if (fullStyle.length > 0 && needsSemicolon(fullStyle)) { |
| 48 | + fullStyle = `${fullStyle};` |
| 49 | + } |
48 | 50 |
|
49 |
| - $(`iframe, object`).attr( |
50 |
| - `style`, |
51 |
| - `${fullStyle} |
52 |
| - position: absolute; |
53 |
| - top: 0; |
54 |
| - left: 0; |
55 |
| - width: 100%; |
56 |
| - height: 100%; |
57 |
| - ` |
58 |
| - ) |
59 |
| - $(`iframe, object`) |
60 |
| - .attr(`width`, null) |
61 |
| - .attr(`height`, null) |
62 |
| - const newIframe = $(`body`).html() // fix for cheerio v1 |
| 51 | + $(`iframe, object`) |
| 52 | + .attr( |
| 53 | + `style`, |
| 54 | + `${fullStyle} |
| 55 | + position: absolute; |
| 56 | + top: 0; |
| 57 | + left: 0; |
| 58 | + width: 100%; |
| 59 | + height: 100%; |
| 60 | + ` |
| 61 | + ) |
| 62 | + .attr(`width`, null) |
| 63 | + .attr(`height`, null) |
63 | 64 |
|
64 |
| - // TODO add youtube preview image as background-image. |
| 65 | + const newIframe = $(`body`).html() // fix for cheerio v1 |
65 | 66 |
|
66 |
| - const rawHTML = oneLine` |
67 |
| - <div |
68 |
| - class="gatsby-resp-iframe-wrapper" |
69 |
| - style="padding-bottom: ${(height / width) * |
70 |
| - 100}%; position: relative; height: 0; overflow: hidden;${ |
71 |
| - options.wrapperStyle |
72 |
| - }" |
73 |
| - > |
74 |
| - ${newIframe} |
75 |
| - </div> |
76 |
| - ` |
| 67 | + // TODO add youtube preview image as background-image. |
77 | 68 |
|
78 |
| - node.type = `html` |
79 |
| - node.value = rawHTML |
80 |
| - } |
81 |
| - } |
82 |
| - }) |
| 69 | + const rawHTML = oneLine` |
| 70 | + <div |
| 71 | + class="gatsby-resp-iframe-wrapper" |
| 72 | + style="padding-bottom: ${(height / width) * |
| 73 | + 100}%; position: relative; height: 0; overflow: hidden; |
| 74 | + ${options.wrapperStyle}" |
| 75 | + > |
| 76 | + ${newIframe} |
| 77 | + </div> |
| 78 | + ` |
83 | 79 |
|
84 |
| - return resolve(markdownAST) |
| 80 | + node.type = `html` |
| 81 | + node.value = rawHTML |
85 | 82 | })
|
| 83 | + |
| 84 | + return markdownAST |
| 85 | +} |
0 commit comments