markdown-it-html5-embed
Advanced tools
Comparing version 0.5.0 to 1.0.0
{ | ||
"name": "markdown-it-html5-embed", | ||
"version": "0.5.0", | ||
"version": "1.0.0", | ||
"homepage": "https://github.com/cmrd-senya/markdown-it-html5-embed", | ||
@@ -19,3 +19,3 @@ "authors": [ | ||
], | ||
"license": "MIT", | ||
"license": "MPLv2", | ||
"ignore": [ | ||
@@ -22,0 +22,0 @@ "**/.*", |
@@ -0,1 +1,7 @@ | ||
## 1.0.0 | ||
- Drop explicit support for Handlebars | ||
- Instead support overriding of a built-in render function | ||
- Allow enabling/disabling embeds with `http://` URLs. Default is disable. | ||
## 0.5.0 | ||
@@ -2,0 +8,0 @@ |
@@ -1,2 +0,2 @@ | ||
/*! markdown-it-html5-embed https://github.com/cmrd-senya/markdown-it-html5-embed @license MIT */ | ||
/*! markdown-it-html5-embed https://github.com/cmrd-senya/markdown-it-html5-embed @license MPLv2 */ | ||
// This is a plugin for markdown-it which adds support for embedding audio/video in the HTML5 way. | ||
@@ -18,3 +18,3 @@ | ||
// The "untitled video" / "untitled audio" messages are only relevant to usage | ||
// inside Handlebars templates, where you can access the title between [] as | ||
// inside alternative render functions, where you can access the title between [] as | ||
// {{title}}, and this text is used if no title is provided. | ||
@@ -73,3 +73,3 @@ var messages = { | ||
if (parsed.mediaType !== null) { | ||
// For use as titles in handlebars templates, we store the description | ||
// For use as titles in alternative render functions, we store the description | ||
// in parsed.title. For use as fallback text, we store it in parsed.fallback | ||
@@ -104,37 +104,27 @@ // alongside the standard fallback text. | ||
function renderMediaEmbed(parsed, options) { | ||
var attributes = options.attributes[parsed.mediaType]; | ||
var useHandlebars = false; | ||
if (options.templateName) { | ||
if (typeof HandlebarsTemplates === "undefined") { | ||
console.log("handlebars_assets is not on the assets pipeline; fall back to the usual mode"); | ||
} else { | ||
useHandlebars = true; | ||
} | ||
function isAllowedSchema(parsed, options) { | ||
if (!options.isAllowedHttp && parsed.url.match('^http://')) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
if (useHandlebars) { | ||
return HandlebarsTemplates[options.templateName]({ | ||
media_type: parsed.mediaType, | ||
attributes: attributes, | ||
mimetype: parsed.mimeType, | ||
source_url: parsed.url, | ||
title: parsed.title, | ||
fallback: parsed.fallback, | ||
needs_cover: parsed.mediaType === "video" | ||
}); | ||
} else { | ||
return ['<' + parsed.mediaType + ' ' + attributes + '>', | ||
'<source type="' + parsed.mimeType + '" src="' + parsed.url + '"></source>', | ||
parsed.fallback, | ||
'</' + parsed.mediaType + '>' | ||
].join('\n'); | ||
} | ||
function isAllowedToEmbed(parsed, options) { | ||
return isAllowedMimeType(parsed, options) && isAllowedSchema(parsed, options); | ||
} | ||
function renderMediaEmbed(parsed, mediaAttributes) { | ||
var attributes = mediaAttributes[parsed.mediaType]; | ||
return ['<' + parsed.mediaType + ' ' + attributes + '>', | ||
'<source type="' + parsed.mimeType + '" src="' + parsed.url + '"></source>', | ||
parsed.fallback, | ||
'</' + parsed.mediaType + '>' | ||
].join('\n'); | ||
} | ||
function html5EmbedRenderer(tokens, idx, options, env, renderer, defaultRender) { | ||
var parsed = parseToken(tokens, idx, env); | ||
if (!isAllowedMimeType(parsed, options.html5embed)) { | ||
if (!isAllowedToEmbed(parsed, options.html5embed)) { | ||
return defaultRender(tokens, idx, options, env, renderer); | ||
@@ -147,3 +137,3 @@ } | ||
return renderMediaEmbed(parsed, options.html5embed); | ||
return renderMediaEmbed(parsed, options.html5embed.attributes); | ||
} | ||
@@ -247,7 +237,7 @@ | ||
if (!isAllowedMimeType(parsed, options)) { | ||
if (!isAllowedToEmbed(parsed, options)) { | ||
return; | ||
} | ||
result += renderMediaEmbed(parsed, options); | ||
result += renderMediaEmbed(parsed, options.attributes); | ||
}); | ||
@@ -298,2 +288,6 @@ if (result.length) { | ||
translate.bind(options.messages); | ||
if (typeof options.renderFn == 'function') { | ||
renderMediaEmbed = options.renderFn; | ||
} | ||
}; |
{ | ||
"name": "markdown-it-html5-embed", | ||
"version": "0.5.0", | ||
"version": "1.0.0", | ||
"description": "This is a plugin for markdown-it which adds support for embedding audio/video in the HTML5 way, by using <video>/<audio> tags.", | ||
@@ -19,3 +19,3 @@ "main": "lib/index.js", | ||
"author": "comrade Senya <senya@riseup.net>", | ||
"license": "MIT", | ||
"license": "MPLv2", | ||
"bugs": { | ||
@@ -22,0 +22,0 @@ "url": "https://github.com/cmrd-senya/markdown-it-html5-embed/issues" |
# markdown-it-html5-embed | ||
This is a plugin for markdown-it which adds support for embedding audio/video in the HTML5 way, by using <video>/<audio> tags. | ||
This is a plugin for markdown-it which adds support for embedding audio/video in the HTML5 way, by using `<video>`/`<audio>` tags. | ||
@@ -156,12 +156,29 @@ ## Install | ||
### Handlebars templates | ||
### Alternative render functions | ||
Options: | ||
```js | ||
templateName: "media-embed_tpl" | ||
renderFn: function(properties, attributes) {/* ... */} | ||
``` | ||
If you want to render media embed using Handlebars template, you can set `templateName` option and the plugin will try to find | ||
the template using global `HandlebarsTemplates` array and render using this template. | ||
By default the plugin renders media with just plain html media tags. However you may want to customize media rendering appropriate for your front-end framework or approach. | ||
You can do this by defining a custom rendering function. | ||
For example, here is a function you can use to render a media embed with Handlebars: | ||
```js | ||
function handleBarsRenderFn(properties, attributes) { | ||
return HandlebarsTemplates[this]({ | ||
media_type: properties.mediaType, | ||
attributes: attributes, | ||
mimetype: properties.mimeType, | ||
source_url: properties.url, | ||
title: properties.title, | ||
fallback: properties.fallback, | ||
needs_cover: properties.mediaType === "video" | ||
}); | ||
} | ||
``` | ||
You can access the descriptive content (e.g., "test link" above) via the | ||
@@ -173,2 +190,11 @@ `{{title}}` variable. It will be set to "Untitled video" or "Untitled audio" | ||
Then you can just set this function as a `renderFn` option on the plugin initialization: | ||
```js | ||
// ... options setting code ... | ||
renderFn: handleBarsRenderFn.bind("templateName"), | ||
``` | ||
More on render function arguments see at the [`renderFn`](#renderFn) option description. | ||
## Options reference | ||
@@ -216,2 +242,8 @@ | ||
#### isAllowedHttp | ||
Boolean. When `true` embed media with `http://` schema in URLs. When `false` ignore and don't count as embeddable media. | ||
Default: `false`. | ||
#### isAllowedMimeType | ||
@@ -238,10 +270,24 @@ | ||
#### templateName | ||
#### renderFn | ||
String. If the plugin is used in a Rails asset pipeline along with the handlebars_assets gem, then you can use a Handlebars template to control the output of the plugin. This option specifies the name of the template to use, which will be picked from the HandlebarsTemplates array. | ||
Function. Override the built-in render function. The function accepts exactly 2 arguments. | ||
If HandlebarsTemplates is undefined, this option is ignored. | ||
```js | ||
function customRenderFn(properties, attributes) { /* ... */ } | ||
``` | ||
Default: `undefined`, don't use Handlebars. | ||
`properties` is an `Object` which contains properties of a detected and parsed media reference. `properties` `Object` contains following keys: | ||
key|type|meaning | ||
-|- | ||
fallback|String|Fallback text for the case when the client (e.g. browser) doesn't support HTML5 `<audio>`/`<video>` | ||
mediaType|String|`"video"` or `"audio"` | ||
mimeType|String|Media mime type resolved by a URL ending (media file extension) | ||
title|String|Title for the media (which could be optionally provided in the markup) | ||
url|String|Media URL | ||
`attributes` is the attributes passed from the plugin options, see [`attributes`](#attributes) option description. | ||
A custom render function must return a `String` value which contains HTML for embedding at the appropriate place. | ||
#### useImageSyntax | ||
@@ -248,0 +294,0 @@ |
@@ -6,2 +6,7 @@ 'use strict'; | ||
function clearBindings() { | ||
// Don't re-use cached function with old bindings | ||
delete require.cache[require.resolve('../lib')]; | ||
} | ||
// For testing custom messages and translations | ||
@@ -62,11 +67,24 @@ var customMessages = { | ||
describe('markdown-it-html5-embed with handlebars', function() { | ||
before(function() { | ||
var Handlebars = require("handlebars"); | ||
global.HandlebarsTemplates = { "template": Handlebars.compile("<h1>{{title}}</h1><div class=\"body\"><{{media_type}} {{attributes}}><source type=\"{{mimetype}}\" src=\"{{source_url}}\"/></{{media_type}}></div>") }; | ||
}); | ||
clearBindings(); | ||
var Handlebars = require("handlebars"); | ||
global.HandlebarsTemplates = { "template": Handlebars.compile("<h1>{{title}}</h1><div class=\"body\"><{{media_type}} {{attributes}}><source type=\"{{mimetype}}\" src=\"{{source_url}}\"/></{{media_type}}></div>") }; | ||
function handleBarsRenderFn(parsed, mediaAttributes) { | ||
var attributes = mediaAttributes[parsed.mediaType]; | ||
return HandlebarsTemplates[this]({ | ||
media_type: parsed.mediaType, | ||
attributes: attributes, | ||
mimetype: parsed.mimeType, | ||
source_url: parsed.url, | ||
title: parsed.title, | ||
fallback: parsed.fallback, | ||
needs_cover: parsed.mediaType === "video" | ||
}); | ||
} | ||
var option = { | ||
html5embed: { | ||
useLinkSyntax: true, | ||
templateName: "template", | ||
renderFn: handleBarsRenderFn.bind("template"), | ||
attributes: { | ||
@@ -82,2 +100,4 @@ "video": "", | ||
generate(path.join(__dirname, 'fixtures/with-handlebars.txt'), md); | ||
clearBindings(); | ||
}); | ||
@@ -121,5 +141,5 @@ | ||
}; | ||
// Don't re-use cached function with old bindings | ||
delete require.cache[require.resolve('../lib')]; | ||
clearBindings(); | ||
var md = require('markdown-it')().use(require('../lib'), option); | ||
@@ -152,4 +172,3 @@ generate(path.join(__dirname, 'fixtures/image-syntax-custom-messages.txt'), md); | ||
// Don't re-use cached function with old bindings | ||
delete require.cache[require.resolve('../lib')]; | ||
clearBindings(); | ||
@@ -167,1 +186,26 @@ var md = require('markdown-it')().use(require('../lib'), option); | ||
}); | ||
describe('markdown-it-html5-embed with link syntax http link when http disabled', function() { | ||
clearBindings(); | ||
var options = { | ||
html5embed: { | ||
useLinkSyntax: true | ||
} | ||
}; | ||
var md = require('markdown-it')().use(require('../lib'), options); | ||
generate(path.join(__dirname, 'fixtures/link-syntax-http-disabled.txt'), md); | ||
}); | ||
describe('markdown-it-html5-embed with link syntax http link when http disabled', function() { | ||
var options = { | ||
html5embed: { | ||
useLinkSyntax: true, | ||
isAllowedHttp: true | ||
} | ||
}; | ||
var md = require('markdown-it')().use(require('../lib'), options); | ||
generate(path.join(__dirname, 'fixtures/link-syntax-http-enabled.txt'), md); | ||
}); |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Copyleft License
License(Experimental) Copyleft license information was found.
Found 1 instance in 1 package
Non-permissive License
License(Experimental) A license not known to be considered permissive was found.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
245526
26
7471
1
329
2
70