UNPKG

@diplodoc/transform

Version:

A simple transformer of text in YFM (Yandex Flavored Markdown) to HTML

117 lines 4.77 kB
"use strict"; // Process @[youtube](youtubeVideoID) // Process @[vimeo](vimeoVideoID) // Process @[vine](vineVideoID) // Process @[prezi](preziID) // Process @[osf](guid) // Process @[yandex](videoID) // Process @[vk](videoID) // Process @[rutube](videoID) // Process @[url](fullLink) const utils_1 = require("../utils"); const parsers_1 = require("./parsers"); const const_1 = require("./const"); // eslint-disable-next-line valid-jsdoc /** * Video plugin for markdown-it. * Forked from https://github.com/CenterForOpenScience/markdown-it-video/tree/0.6.3 */ const video = (md, options) => { const theOptions = Object.assign(Object.assign({}, const_1.defaults), options); const theMd = md; theMd.renderer.rules.video = tokenizeVideo(theMd, theOptions); theMd.inline.ruler.before('emphasis', 'video', videoEmbed(theMd, theOptions)); }; function tokenizeVideo(md, options) { return (tokens, idx) => { const videoID = md.utils.escapeHtml(tokens[idx].videoID); const service = md.utils.escapeHtml(tokens[idx].service).toLowerCase(); const checkUrl = /http(?:s?):\/\/(?:www\.)?[a-zA-Z0-9-:.]{1,}\/render(?:\/)?[a-zA-Z0-9.&;?=:%]{1,}url=http(?:s?):\/\/[a-zA-Z0-9 -:.]{1,}\/[a-zA-Z0-9]{1,5}\/\?[a-zA-Z0-9.=:%]{1,}/; let num; if (service === const_1.VideoService.Osf && videoID) { num = Math.random() * 0x10000; if (videoID.match(checkUrl)) { return ('<div id="' + num + '" class="mfr mfr-file"></div><script>' + '$(document).ready(function () {new mfr.Render("' + num + '", "' + videoID + '");' + ' }); </script>'); } return ('<div id="' + num + '" class="mfr mfr-file"></div><script>' + '$(document).ready(function () {new mfr.Render("' + num + '", "https://mfr.osf.io/' + 'render?url=https://osf.io/' + videoID + '/?action=download%26mode=render");' + ' }); </script>'); } const { width, height } = options[service]; return videoID === '' ? '' : `<div class="embed-responsive embed-responsive-16by9"><iframe` + ` class="embed-responsive-item ${service}-player"` + ` type="text/html" width="${width}" height="${height}"` + ` src="${options.videoUrl(service, videoID, options)}"` + ` frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe></div>`; }; } const EMBED_REGEX = /@\[([a-zA-Z]*?)]\([\s]*(.*?)[\s]*[)]/im; function videoEmbed(md, _options) { return (state, silent) => { var _a; var _b; const theState = state; const oldPos = state.pos; if (state.src.charCodeAt(oldPos) !== 0x40 /* @ */ || state.src.charCodeAt(oldPos + 1) !== 0x5b /* [ */) { return false; } const match = EMBED_REGEX.exec(state.src.slice(state.pos, state.src.length)); if (!match || match.length < 3) { return false; } const service = match[1] || 'url'; const parsed = (0, parsers_1.parseVideoUrl)(service, match[2]); if (parsed === false) { return false; } const [videoID, csp] = parsed; const serviceStart = oldPos + 2; const serviceEnd = md.helpers.parseLinkLabel(state, oldPos + 1, false); // // We found the end of the link, and know for a fact it's a valid link; // so all that's left to do is to call tokenizer. // if (!silent) { theState.pos = serviceStart; // @ts-expect-error theState.service = theState.src.slice(serviceStart, serviceEnd); const newState = new theState.md.inline.State(service, theState.md, theState.env, []); newState.md.inline.tokenize(newState); const token = theState.push('video', '', 0); token.videoID = videoID; token.service = service; token.level = theState.level; } if (service === 'url') { theState.pos = theState.src.indexOf(')', theState.pos) + 1; } else { theState.pos += theState.src.indexOf(')', theState.pos); } if (csp) { (_a = (_b = state.env).meta) !== null && _a !== void 0 ? _a : (_b.meta = {}); (0, utils_1.append)(state.env.meta, 'csp', csp); } return true; }; } module.exports = video; //# sourceMappingURL=index.js.map