From 218cab051bfee550fe76d69e6658f1d2a1569346 Mon Sep 17 00:00:00 2001 From: Shibasis Date: Wed, 26 Oct 2016 08:23:54 +0530 Subject: [PATCH] solved #274 review youtube embed handling --- lib/plugin/youtube.js | 101 ------------------------------------------ lib/render.js | 2 - 2 files changed, 103 deletions(-) delete mode 100644 lib/plugin/youtube.js diff --git a/lib/plugin/youtube.js b/lib/plugin/youtube.js deleted file mode 100644 index 5bb2869..0000000 --- a/lib/plugin/youtube.js +++ /dev/null @@ -1,101 +0,0 @@ -// markdown-it plugin to wrap youtube iframes in a
for styling purposes -// -var URL = require('url') - -var WRAPPER_START = "
" -var WRAPPER_END = '
' - -// determine whether the given HTML string contains an iframe pointing to a youtube.com URL -function isYoutubeIframe (content) { - // look for the src attribute's value - var attr = content.match(/<\s*iframe[^>]*\bsrc\s*=\s*/) - if (attr) { - // mark the location and figure out what kind of quotation mark is delimiting the value - var position = attr.index + attr[0].length - var quote = content.charAt(position) - var substring = content.slice(position + 1) - - // now that we've found the first delimiting quotation mark, match - // everything up to the next instance of the same quotation mark - var src = substring.match(new RegExp('^[^' + quote + ']*')) - if (src) { - var value = src[0] - // for protocol-relative src, prepend a protocol for URL parsing purposes - if (value.indexOf('//') === 0) { - value = 'https:' + value - } - - var url = URL.parse(value) - return (url.host && url.host.match(/^(\w+\.)?youtube\.com$/)) - } - } - return false -} - -module.exports = function (md, opts) { - // Wrap iframes that appear in HTML blocks - // - // In html_block tokens, the entire contents of an HTML block appear as the - // `.content` property of a single token object. For a standalone `` tag, so we can find the end of it - var closingMatch = content.substring(start).match(/>[^>]*<\s*\/\s*iframe\s*>/) - if (closingMatch) { - var endOffset = content.substring(start + closingMatch.index + 1).indexOf('>') + 1 - end = start + closingMatch.index + endOffset + 1 - } - - // slice up the content according to the positions we've computed - var prefix = content.substring(0, start) - var iframe = content.substring(start, end) - var postfix = content.substring(end) - - // inject the wrapper element - tokens[idx].content = prefix + WRAPPER_START + iframe + WRAPPER_END + postfix - } - - return originalBlockRule.call(this, tokens, idx, options, env, self) - } - - // Wrap iframes that appear inside inline HTML strings - // - // In runs of html_inline tokens, the opening `` tag, so what we have to do if we - // find a match is walk through the list to find out where to put the closing - // `
` part of our wrapper - // - var originalInlineRule = md.renderer.rules.html_inline - md.renderer.rules.html_inline = function (tokens, idx, options, env, self) { - if (isYoutubeIframe(tokens[idx].content)) { - // prepend the opening part of the wrapper - tokens[idx].content = WRAPPER_START + tokens[idx].content - - // find the closing tag and append the closing part of the wrapper - for (var position = idx; position < tokens.length && tokens[position].type === 'html_inline'; position++) { - if (tokens[position].content === '') { - tokens[position].content += WRAPPER_END - } - } - } - - return originalInlineRule.call(this, tokens, idx, options, env, self) - } -} diff --git a/lib/render.js b/lib/render.js index ef09457..980e675 100644 --- a/lib/render.js +++ b/lib/render.js @@ -12,7 +12,6 @@ var codeWrap = require('./plugin/code-wrap') var headingLinks = require('./plugin/heading-links') var gravatar = require('./plugin/gravatar') var github = require('./plugin/github') -var youtube = require('./plugin/youtube') var cdnImages = require('./plugin/cdn') var badges = require('./plugin/badges') var packagize = require('./plugin/packagize') @@ -72,7 +71,6 @@ var render = module.exports = function (html, options) { .use(relaxedLinkRefs) .use(gravatar) .use(github, {package: options.package}) - .use(youtube) .use(badges) .use(packagize, {package: options.package})