@atlaskit/editor-wikimarkup-transformer
Version:
Wiki markup transformer for JIRA and Confluence
51 lines (49 loc) • 1.79 kB
JavaScript
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
// Ignored via go/ees007
// eslint-disable-next-line @atlaskit/editor/enforce-todo-comment-format
// TODO: Create a type for rawContentProcessor which will be shared among parsers
export function commonMacro(input, schema, opt) {
/**
* Forging the opening regex, the result would look something like
* /^\{(quote)(?::([^\{\n\}]*))?\}/i
*/
// Ignored via go/ees005
// eslint-disable-next-line require-unicode-regexp
var opening = new RegExp("^{(".concat(opt.keyword, ")(?::([^{\n}]*))?}"), 'i');
var matchOpening = input.match(opening);
if (!matchOpening) {
return fallback(input);
}
var _matchOpening = _slicedToArray(matchOpening, 3),
name = _matchOpening[1],
rawAttrs = _matchOpening[2];
var openingLength = matchOpening[0].length;
if (!opt.paired) {
/**
* Some macros do not have a closing symbol, for example
* {anchor:here} {loremipsum}
*/
return opt.rawContentProcessor(rawAttrs, '', openingLength, schema, opt.context);
}
/**
* Forging the closing regex, the result would look something like
* /\{quote\}/
*/
// Ignored via go/ees005
// eslint-disable-next-line require-unicode-regexp
var closing = new RegExp("{".concat(name, "}"));
var matchClosing = closing.exec(input.substring(openingLength));
var rawContent = '';
if (matchClosing) {
rawContent = input.substring(openingLength, openingLength + matchClosing.index);
}
var length = matchClosing ? openingLength + matchClosing.index + matchClosing[0].length : openingLength;
return opt.rawContentProcessor(rawAttrs, rawContent, length, schema, opt.context);
}
function fallback(input) {
return {
type: 'text',
text: input.substr(0, 1),
length: 1
};
}