Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebComponent support and activate nested parsing #413

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"conventionalCommits.scopes": [
"htmlblock"
]
}
59 changes: 58 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,64 @@ var md = new Remarkable({

// Highlighter function. Should return escaped HTML,
// or '' if the source string is not changed
highlight: function (/*str, lang*/) { return ''; }
highlight: function (/*str, lang*/) { return ''; },

// Allow / Disallow html tags that are available for parsing HTML
htmlAllowedTags: [
/^my-.*/,
'my-web-component', // Custom-tag

'article',
'aside',
'button',
'blockquote',
'body',
'canvas',
'caption',
'col',
'colgroup',
'dd',
'div',
'dl',
'dt',
'embed',
'fieldset',
'figcaption',
'figure',
'footer',
'form',
'h1',
'h2',
'h3',
'h4',
'h5',
'h6',
'header',
'hgroup',
'hr',
'iframe',
'li',
'map',
'object',
'ol',
'output',
'p',
'pre',
'progress',
'script',
'section',
'style',
'table',
'tbody',
'td',
'textarea',
'tfoot',
'th',
'tr',
'thead',
'ul',
'video'
]
});

console.log(md.render('# Remarkable rulezz!'));
Expand Down
31 changes: 26 additions & 5 deletions lib/rules_block/htmlblock.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// HTML block

import block_names from '../common/html_blocks';
import html_blocks from '../common/html_blocks';


var HTML_TAG_OPEN_RE = /^<([a-zA-Z]{1,15})[\s\/>]/;
var HTML_TAG_CLOSE_RE = /^<\/([a-zA-Z]{1,15})[\s>]/;
var HTML_TAG_OPEN_RE = /^<([a-zA-Z_-]{1,15})[\s\/>]/;
var HTML_TAG_CLOSE_RE = /^<\/([a-zA-Z_-]{1,15})[\s>]/;

function isLetter(ch) {
/*eslint no-bitwise:0*/
Expand All @@ -20,9 +20,19 @@ export default function htmlblock(state, startLine, endLine, silent) {

pos += shift;

let allowedBlocks = html_blocks;

if (!state.options.html) { return false; }

if (shift > 3 || pos + 2 >= max) { return false; }
if (!Array.isArray(allowedBlocks)) {
allowedBlocks = Object.keys(allowedBlocks)
}

if (Array.isArray(state.options.htmlAllowedTags)) {
allowedBlocks = state.options.htmlAllowedTags;
}

if (shift > 3 + state.blkIndent || pos + 2 >= max) { return false; }

if (state.src.charCodeAt(pos) !== 0x3C/* < */) { return false; }

Expand All @@ -44,8 +54,19 @@ export default function htmlblock(state, startLine, endLine, silent) {
match = state.src.slice(pos, max).match(HTML_TAG_OPEN_RE);
if (!match) { return false; }
}

// Make sure tag name is valid
if (block_names[match[1].toLowerCase()] !== true) { return false; }
const tagToCheck = match[1].toLowerCase();
if (!allowedBlocks.includes(tagToCheck)) {
const regExpressions = allowedBlocks
.filter(possibleExpression => typeof possibleExpression !== 'string')
.filter(possibleExpression => !!possibleExpression.test);

if (!regExpressions.some(x => x.test(tagToCheck))) {
return false;
}
}

if (silent) { return true; }

} else {
Expand Down