Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return tokenizer;
}
// decorate for all block tokenizers
Object.keys(blockTokenizers).forEach(method => {
blockTokenizers[method] = tokenizerDecorator(blockTokenizers[method]);
});
// decorate for all inline tokenizers
Object.keys(inlineTokenizers).forEach(method => {
inlineTokenizers[method] = tokenizerDecorator(inlineTokenizers[method]);
});
// proxy set options to avoid remove the custom strategy option
(parse.Parser.prototype as any).setOptions = function(opts) {
if (this.options.strategy) {
opts.strategy = this.options.strategy;
}
setOptions.call(this, opts);
};
export default parse as Plugin<[Partial?]>;
const parse = (mdx, options = {}) => {
options.components = options.components || {}
// TODO: Need to figure out a better way to handle imports
// parsing. As implemented it parses the whole thing :(
options.blocks = Object
.keys(options.components)
.concat(getImports(mdx).scope)
.concat(blocks)
const fn = unified()
.use(remark, options)
.use(matter, { type: 'yaml', marker: '-' })
.use(parseImports, options)
.use(jsx, options)
.use(rehype)
.use(html)
return fn.processSync(mdx)
}
.map(i => {
const squeezed = i.value.replace(/\s+/g, ' ').split(' import').join('\nimport')
const parsed = parseImports(squeezed)
return {
raw: i.value,
parsed
}
})
const scope = importScope(imports)
return {
imports,
scope,
blocks: blocks.concat(scope)
}
}
import parse, { RemarkParseOptions } from 'remark-parse';
import { Plugin } from 'unified';
export interface IParseProps extends RemarkParseOptions {
/**
* transform strategy
* @note turn on all markdown tokenizers by default
* only turn on tokenizers which use for parse yaml data if pass 'data'
*/
strategy: 'default' | 'data';
}
const { blockTokenizers, inlineTokenizers, setOptions } = parse.Parser.prototype as any;
const oFencedCode = blockTokenizers.fencedCode;
const DISABLEABLE_TOKENIZERS = [
'indentedCode',
'fencedCode',
'blockquote',
'thematicBreak',
'list',
'setextHeading',
'html',
'footnote',
'definition',
'table',
'escape',
'autoLink',
'url',
'html',
export function parse(markdown: string): Root {
const parser = new Parser(null, vfile(markdown));
const parsed = parser.parse();
clean(parsed);
return parsed;
}
function smartHtmlParser(componentWhitelist) {
var components = blockElements.concat(componentWhitelist).join('|');
function parseOpeningTags(valueDesc) {
return partialTokenizer(function (valueDesc) {
var regexp = '<(' + components + ')(' + attribute + '*)\\s*(/?)>';
var propertiesRegex = '(' + attributeName + ')(?:\\s*=\\s*' + attributeValue + ')?';
var matches = getAllMatches(regexp, valueDesc.value).map(function (match) {
return {
value: match[0],
type: match[8] ? 'autoCloseTag' : 'openTag',
tagName: match[1],
startsAt: match.index + valueDesc.startsAt,
endsAt: valueDesc.startsAt + match.index + match[0].length,
properties: getAllMatches(propertiesRegex, match[2]).reduce(function (props, m) {
var value = m[2] || m[3] || m[5];
var quote = m[4] || m[6];
var unescapedValue = quote ? value.replace(new RegExp('\\\\' + quote, 'g'), quote) : value;