@botonic/plugin-contentful
Version:
Botonic Plugin Contentful is one of the **[available](https://github.com/hubtype/botonic/tree/master/packages)** plugins for Botonic. **[Contentful](http://www.contentful.com)** is a CMS (Content Management System) which manages contents of a great variet
83 lines • 2.85 kB
JavaScript
import escapeStringRegexp from 'escape-string-regexp';
export var MarkupType;
(function (MarkupType) {
/**
* CommonMark markdown
* https://spec.commonmark.org/
* Italics is *text* or _text_
* Bold is **text** or __text__
* */
MarkupType["MARKDOWN"] = "markdown";
/**
* Bold is *text* and italics is _text_
* https://faq.whatsapp.com/en/android/26000002/
**/
MarkupType["WHATSAPP"] = "whatsapp";
/**
* https://guides.github.com/pdfs/markdown-cheatsheet-online.pdf
* https://guides.github.com/features/mastering-markdown/
*/
MarkupType["GITHUB"] = "github";
/**
* Contentful supports github flavoured markdown. Its editor by default generates:
* Italics with *text*
* Bold with __text__
*/
MarkupType["CONTENTFUL"] = "contentful";
})(MarkupType || (MarkupType = {}));
export var TokenType;
(function (TokenType) {
TokenType["STRONG"] = "strong";
TokenType["EMPHASIS"] = "em";
TokenType["ESCAPE"] = "escape";
TokenType["PARAGRAPH"] = "paragraph";
TokenType["TEXT"] = "text";
TokenType["SPACE"] = "space";
TokenType["CODE"] = "code";
TokenType["HEADING"] = "heading";
// TODO add others
})(TokenType || (TokenType = {}));
export class MarkUp {
constructor(type) {
this.type = type;
}
render(tokens, separator = '\n') {
return tokens.map(t => this.renderToken(t)).join(separator);
// let render = ''
// visitTokens(tokens, t => render += this.renderToken())
}
/** Remove an inline content (https://spec.commonmark.org/0.29/#inlines)
* eg. disableInlines('_hi_', EMPHASIS) = 'hi' */
disableInlines(input, inlineTypes) {
// Ideally we should be able to modify the token tree and re-render, but I didn't
// find any js library able to render markdown (they always render html)
const tokens = this.parse(input);
const bolds = recursiveTokenFind(tokens, t => inlineTypes.includes(t.type));
for (const bold of bolds) {
const MARKER = escapeStringRegexp(bold.raw[0]);
const regex = new RegExp(`(?<!${MARKER})${escapeStringRegexp(bold.raw)}(?!${MARKER})`);
input = input.replace(regex, bold.tokens[0].text);
}
return input;
}
}
export function visitTokens(input, visitor) {
for (const token of input) {
if (token.items) {
token.items.forEach(t => visitTokens(t.tokens, visitor));
}
else if (token.tokens) {
visitTokens(token.tokens, visitor);
}
visitor(token);
}
}
export function recursiveTokenFind(tokens, predicate) {
const out = [];
visitTokens(tokens, (t) => {
if (predicate(t))
out.push(t);
});
return out;
}
//# sourceMappingURL=markup.js.map