@intlayer/core
Version:
Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.
339 lines (337 loc) • 12.4 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
//#region src/markdown/constants.ts
/**
* Analogous to `node.type`. Please note that the values here may change at any time,
* so do not hard code against the value directly.
*/
const RuleType = {
blockQuote: "0",
breakLine: "1",
breakThematic: "2",
codeBlock: "3",
codeFenced: "4",
codeInline: "5",
footnote: "6",
footnoteReference: "7",
gfmTask: "8",
heading: "9",
headingSetext: "10",
/** only available if not `disableHTMLParsing` */
htmlBlock: "11",
htmlComment: "12",
/** only available if not `disableHTMLParsing` */
htmlSelfClosing: "13",
/** Custom components like <Tabs>, <TabItem> */
customComponent: "34",
image: "14",
link: "15",
/** emits a `link` 'node', does not render directly */
linkAngleBraceStyleDetector: "16",
/** emits a `link` 'node', does not render directly */
linkBareUrlDetector: "17",
newlineCoalescer: "19",
orderedList: "20",
paragraph: "21",
ref: "22",
refImage: "23",
refLink: "24",
table: "25",
tableSeparator: "26",
text: "27",
textBolded: "28",
textEmphasized: "29",
textEscaped: "30",
textMarked: "31",
textStrikethroughed: "32",
unorderedList: "33"
};
if (process.env.NODE_ENV === "test") Object.keys(RuleType).forEach((key) => {
RuleType[key] = key;
});
/**
* Priority levels for rule ordering.
*/
const Priority = {
/** anything that must scan the tree before everything else */
MAX: 0,
/** scans for block-level constructs */
HIGH: 1,
/** inline w/ more priority than other inline */
MED: 2,
/** inline elements */
LOW: 3,
/** bare text and stuff that is considered leftovers */
MIN: 4
};
/** Threshold for performance logging (in milliseconds) */
const DURATION_DELAY_TRIGGER = 20;
/**
* Map of HTML attributes to their JSX prop equivalents.
* Some renderers use camelCase for certain attributes.
*/
const ATTRIBUTE_TO_NODE_PROP_MAP = [
"allowFullScreen",
"allowTransparency",
"autoComplete",
"autoFocus",
"autoPlay",
"cellPadding",
"cellSpacing",
"charSet",
"classId",
"colSpan",
"contentEditable",
"contextMenu",
"crossOrigin",
"encType",
"formAction",
"formEncType",
"formMethod",
"formNoValidate",
"formTarget",
"frameBorder",
"hrefLang",
"inputMode",
"keyParams",
"keyType",
"marginHeight",
"marginWidth",
"maxLength",
"mediaGroup",
"minLength",
"noValidate",
"radioGroup",
"readOnly",
"rowSpan",
"spellCheck",
"srcDoc",
"srcLang",
"srcSet",
"tabIndex",
"useMap"
].reduce((obj, x) => {
obj[x.toLowerCase()] = x;
return obj;
}, {
class: "className",
for: "htmlFor"
});
/**
* Default HTML entity to unicode mappings.
*/
const NAMED_CODES_TO_UNICODE = {
amp: "&",
apos: "'",
gt: ">",
lt: "<",
nbsp: "\xA0",
quot: "“"
};
/** HTML elements that should not have their content processed */
const DO_NOT_PROCESS_HTML_ELEMENTS = [
"style",
"script",
"pre"
];
/** Attributes that require URL sanitization */
const ATTRIBUTES_TO_SANITIZE = [
"src",
"href",
"data",
"formAction",
"srcDoc",
"action"
];
/** Attribute extractor regex */
const ATTR_EXTRACTOR_R = /([-A-Z0-9_:]+)(?:\s*=\s*(?:(?:"((?:\\.|[^"])*)")|(?:'((?:\\.|[^'])*)')|(?:\{((?:\\.|{[^}]*?}|[^}])*)\})))?/gi;
/** Block end detection */
const BLOCK_END_R = /\n{2,}$/;
/** Blockquote patterns */
const BLOCKQUOTE_R = /^(\s*>[\s\S]*?)(?=\n\n|$)/;
const BLOCKQUOTE_TRIM_LEFT_MULTILINE_R = /^ *> ?/gm;
const BLOCKQUOTE_ALERT_R = /^(?:\[!([^\]]*)\]\n)?([\s\S]*)/;
/** Line break patterns */
const BREAK_LINE_R = /^ {2,}\n/;
const BREAK_THEMATIC_R = /^(?:([-*_])( *\1){2,}) *(?:\n *)+\n/;
/** Code block patterns */
const CODE_BLOCK_FENCED_R = /^(?: {1,3})?(`{3,}|~{3,}) *(\S+)? *([^\n]*?)?\n([\s\S]*?)(?:\1\n?|$)/;
const CODE_BLOCK_R = /^(?: {4}[^\n]+\n*)+(?:\n *)+\n?/;
const CODE_INLINE_R = /^(`+)((?:\\`|(?!\1)`|[^`])+)\1/;
/** Newline patterns */
const CONSECUTIVE_NEWLINE_R = /^(?:\n *)*\n/;
const CR_NEWLINE_R = /\r\n?/g;
/** Footnote patterns */
const FOOTNOTE_R = /^\[\^([^\]]+)](:(.*)((\n+ {4,}.*)|(\n(?!\[\^).+))*)/;
const FOOTNOTE_REFERENCE_R = /^\[\^([^\]]+)]/;
/** Form feed */
const FORMFEED_R = /\f/g;
/** Front matter */
const FRONT_MATTER_R = /^---[ \t]*\n(.|\n)*?\n---[ \t]*\n/;
/** GFM task */
const GFM_TASK_R = /^\s*?\[(x|\s)\]/;
/** Heading patterns */
const HEADING_R = /^ *(#{1,6}) *([^\n]+?)(?: +#*)?(?:\n *)*(?:\n|$)/;
const HEADING_ATX_COMPLIANT_R = /^ *(#{1,6}) +([^\n]+?)(?: +#*)?(?:\n *)*(?:\n|$)/;
const HEADING_SETEXT_R = /^([^\n]+)\n *(=|-)\2{2,} *\n/;
/** HTML patterns */
const HTML_BLOCK_ELEMENT_R = /^ *(?!<[a-zA-Z][^ >/]* ?\/>)<([a-zA-Z][^ >/]*) ?((?:[^>]*[^/])?)>\n?(\s*(?:<\1[^>]*?>[\s\S]*?<\/\1>|(?!<\1\b)[\s\S])*?)<\/\1>(?!<\/\1>)\n*/i;
const HTML_CHAR_CODE_R = /&([a-z0-9]+|#[0-9]{1,6}|#x[0-9a-fA-F]{1,6});/gi;
const HTML_COMMENT_R = /^<!--[\s\S]*?(?:-->)/;
const HTML_CUSTOM_ATTR_R = /^(data|aria|x)-[a-z_][a-z\d_.-]*$/;
const HTML_SELF_CLOSING_ELEMENT_R = /^ *<([a-zA-Z][a-zA-Z0-9:]*)(?:\s+((?:<.*?>|[^>])*))?\/?>(?!<\/\1>)(\s*\n)?/i;
/** Custom component pattern */
const CUSTOM_COMPONENT_R = /^ *<([A-Z][a-zA-Z0-9]*)(?:\s+((?:<.*?>|[^>])*))?>\n?(\s*(?:<\1[^>]*?>[\s\S]*?<\/\1>|(?!<\1\b)[\s\S])*?)<\/\1>(?!<\/\1>)\n*/;
/** Interpolation */
const INTERPOLATION_R = /^\{.*\}$/;
/** Link patterns */
const LINK_AUTOLINK_BARE_URL_R = /^(https?:\/\/[^\s<]+[^<.,:;"')\]\s])/;
const LINK_AUTOLINK_R = /^<([^ >]+[:@/][^ >]+)>/;
const CAPTURE_LETTER_AFTER_HYPHEN = /-([a-z])?/gi;
/** Table patterns */
const NP_TABLE_R = /^(\|.*)\n(?: *(\|? *[-:]+ *\|[-| :]*)\n((?:.*\|.*\n)*))?\n?/;
const TABLE_TRIM_PIPES = /(^ *\||\| *$)/g;
const TABLE_CENTER_ALIGN = /^ *:-+: *$/;
const TABLE_LEFT_ALIGN = /^ *:-+ *$/;
const TABLE_RIGHT_ALIGN = /^ *-+: *$/;
/** Paragraph */
const PARAGRAPH_R = /^[^\n]+(?: {2}\n|\n{2,})/;
/** Reference patterns */
const REFERENCE_IMAGE_OR_LINK = /^\[([^\]]*)\]:\s+<?([^\s>]+)>?\s*("([^"]*)")?/;
const REFERENCE_IMAGE_R = /^!\[([^\]]*)\] ?\[([^\]]*)\]/;
const REFERENCE_LINK_R = /^\[([^\]]*)\] ?\[([^\]]*)\]/;
/** Block detection */
const SHOULD_RENDER_AS_BLOCK_R = /(\n|^[-*]\s|^#|^ {2,}|^-{2,}|^>\s)/;
/** Tab and whitespace */
const TAB_R = /\t/g;
const TRIM_STARTING_NEWLINES = /^\n+/;
const HTML_LEFT_TRIM_AMOUNT_R = /^\n*([ \t]*)/;
/** List patterns */
const LIST_LOOKBEHIND_R = /(?:^|\n)( *)$/;
const ORDERED_LIST_BULLET = "(?:\\d+\\.)";
const UNORDERED_LIST_BULLET = "(?:[*+-])";
/** Text formatting patterns */
const TEXT_ESCAPED_R = /^\\([^0-9A-Za-z\s])/;
const UNESCAPE_R = /\\([^0-9A-Za-z\s])/g;
const TEXT_PLAIN_R = /^[\s\S](?:(?! {2}\n|[0-9]\.|http)[^=*_~\-\n:<`\\[!])*/;
/** Shortcode pattern */
const SHORTCODE_R = /^(:[a-zA-Z0-9-_]+:)/;
const ORDERED = 1;
const UNORDERED = 2;
/**
* Ensure there's at least one more instance of the delimiter later
* in the current sequence.
*/
const LOOKAHEAD = (double) => `(?=[\\s\\S]+?\\1${double ? "\\1" : ""})`;
/**
* For inline formatting, this partial attempts to ignore characters that
* may appear in nested formatting.
*/
const INLINE_SKIP_R = "((?:\\[.*?\\][([].*?[)\\]]|<.*?>(?:.*?<.*?>)?|`.*?`|\\\\[^\\s]|[\\s\\S])+?)";
/** Bold text pattern */
const TEXT_BOLD_R = new RegExp(`^([*_])\\1${LOOKAHEAD(1)}${INLINE_SKIP_R}\\1\\1(?!\\1)`);
/** Emphasized text pattern */
const TEXT_EMPHASIZED_R = new RegExp(`^([*_])${LOOKAHEAD(0)}${INLINE_SKIP_R}\\1(?!\\1)`);
/** Marked text pattern */
const TEXT_MARKED_R = new RegExp(`^(==)${LOOKAHEAD(0)}${INLINE_SKIP_R}\\1`);
/** Strikethrough text pattern */
const TEXT_STRIKETHROUGHED_R = new RegExp(`^(~~)${LOOKAHEAD(0)}${INLINE_SKIP_R}\\1`);
const generateListItemPrefix = (type) => {
return "( *)(" + (type === 1 ? ORDERED_LIST_BULLET : UNORDERED_LIST_BULLET) + ") +";
};
const ORDERED_LIST_ITEM_PREFIX = generateListItemPrefix(1);
const UNORDERED_LIST_ITEM_PREFIX = generateListItemPrefix(2);
const generateListItemPrefixRegex = (type) => {
return new RegExp("^" + (type === 1 ? ORDERED_LIST_ITEM_PREFIX : UNORDERED_LIST_ITEM_PREFIX));
};
const ORDERED_LIST_ITEM_PREFIX_R = generateListItemPrefixRegex(1);
const UNORDERED_LIST_ITEM_PREFIX_R = generateListItemPrefixRegex(2);
const generateListItemRegex = (type) => {
return new RegExp("^" + (type === 1 ? ORDERED_LIST_ITEM_PREFIX : UNORDERED_LIST_ITEM_PREFIX) + "[^\\n]*(?:\\n(?!\\1" + (type === 1 ? ORDERED_LIST_BULLET : UNORDERED_LIST_BULLET) + " )[^\\n]*)*(\\n|$)", "gm");
};
const ORDERED_LIST_ITEM_R = generateListItemRegex(1);
const UNORDERED_LIST_ITEM_R = generateListItemRegex(2);
const generateListRegex = (type) => {
const bullet = type === 1 ? ORDERED_LIST_BULLET : UNORDERED_LIST_BULLET;
return new RegExp("^( *)(" + bullet + ") [\\s\\S]+?(?:\\n{2,}(?! )(?!\\1" + bullet + " (?!" + bullet + " ))\\n*|\\s*\\n*$)");
};
const ORDERED_LIST_R = generateListRegex(1);
const UNORDERED_LIST_R = generateListRegex(2);
//#endregion
exports.ATTRIBUTES_TO_SANITIZE = ATTRIBUTES_TO_SANITIZE;
exports.ATTRIBUTE_TO_NODE_PROP_MAP = ATTRIBUTE_TO_NODE_PROP_MAP;
exports.ATTR_EXTRACTOR_R = ATTR_EXTRACTOR_R;
exports.BLOCKQUOTE_ALERT_R = BLOCKQUOTE_ALERT_R;
exports.BLOCKQUOTE_R = BLOCKQUOTE_R;
exports.BLOCKQUOTE_TRIM_LEFT_MULTILINE_R = BLOCKQUOTE_TRIM_LEFT_MULTILINE_R;
exports.BLOCK_END_R = BLOCK_END_R;
exports.BREAK_LINE_R = BREAK_LINE_R;
exports.BREAK_THEMATIC_R = BREAK_THEMATIC_R;
exports.CAPTURE_LETTER_AFTER_HYPHEN = CAPTURE_LETTER_AFTER_HYPHEN;
exports.CODE_BLOCK_FENCED_R = CODE_BLOCK_FENCED_R;
exports.CODE_BLOCK_R = CODE_BLOCK_R;
exports.CODE_INLINE_R = CODE_INLINE_R;
exports.CONSECUTIVE_NEWLINE_R = CONSECUTIVE_NEWLINE_R;
exports.CR_NEWLINE_R = CR_NEWLINE_R;
exports.CUSTOM_COMPONENT_R = CUSTOM_COMPONENT_R;
exports.DO_NOT_PROCESS_HTML_ELEMENTS = DO_NOT_PROCESS_HTML_ELEMENTS;
exports.DURATION_DELAY_TRIGGER = DURATION_DELAY_TRIGGER;
exports.FOOTNOTE_R = FOOTNOTE_R;
exports.FOOTNOTE_REFERENCE_R = FOOTNOTE_REFERENCE_R;
exports.FORMFEED_R = FORMFEED_R;
exports.FRONT_MATTER_R = FRONT_MATTER_R;
exports.GFM_TASK_R = GFM_TASK_R;
exports.HEADING_ATX_COMPLIANT_R = HEADING_ATX_COMPLIANT_R;
exports.HEADING_R = HEADING_R;
exports.HEADING_SETEXT_R = HEADING_SETEXT_R;
exports.HTML_BLOCK_ELEMENT_R = HTML_BLOCK_ELEMENT_R;
exports.HTML_CHAR_CODE_R = HTML_CHAR_CODE_R;
exports.HTML_COMMENT_R = HTML_COMMENT_R;
exports.HTML_CUSTOM_ATTR_R = HTML_CUSTOM_ATTR_R;
exports.HTML_LEFT_TRIM_AMOUNT_R = HTML_LEFT_TRIM_AMOUNT_R;
exports.HTML_SELF_CLOSING_ELEMENT_R = HTML_SELF_CLOSING_ELEMENT_R;
exports.INLINE_SKIP_R = INLINE_SKIP_R;
exports.INTERPOLATION_R = INTERPOLATION_R;
exports.LINK_AUTOLINK_BARE_URL_R = LINK_AUTOLINK_BARE_URL_R;
exports.LINK_AUTOLINK_R = LINK_AUTOLINK_R;
exports.LIST_LOOKBEHIND_R = LIST_LOOKBEHIND_R;
exports.LOOKAHEAD = LOOKAHEAD;
exports.NAMED_CODES_TO_UNICODE = NAMED_CODES_TO_UNICODE;
exports.NP_TABLE_R = NP_TABLE_R;
exports.ORDERED = ORDERED;
exports.ORDERED_LIST_BULLET = ORDERED_LIST_BULLET;
exports.ORDERED_LIST_ITEM_PREFIX = ORDERED_LIST_ITEM_PREFIX;
exports.ORDERED_LIST_ITEM_PREFIX_R = ORDERED_LIST_ITEM_PREFIX_R;
exports.ORDERED_LIST_ITEM_R = ORDERED_LIST_ITEM_R;
exports.ORDERED_LIST_R = ORDERED_LIST_R;
exports.PARAGRAPH_R = PARAGRAPH_R;
exports.Priority = Priority;
exports.REFERENCE_IMAGE_OR_LINK = REFERENCE_IMAGE_OR_LINK;
exports.REFERENCE_IMAGE_R = REFERENCE_IMAGE_R;
exports.REFERENCE_LINK_R = REFERENCE_LINK_R;
exports.RuleType = RuleType;
exports.SHORTCODE_R = SHORTCODE_R;
exports.SHOULD_RENDER_AS_BLOCK_R = SHOULD_RENDER_AS_BLOCK_R;
exports.TABLE_CENTER_ALIGN = TABLE_CENTER_ALIGN;
exports.TABLE_LEFT_ALIGN = TABLE_LEFT_ALIGN;
exports.TABLE_RIGHT_ALIGN = TABLE_RIGHT_ALIGN;
exports.TABLE_TRIM_PIPES = TABLE_TRIM_PIPES;
exports.TAB_R = TAB_R;
exports.TEXT_BOLD_R = TEXT_BOLD_R;
exports.TEXT_EMPHASIZED_R = TEXT_EMPHASIZED_R;
exports.TEXT_ESCAPED_R = TEXT_ESCAPED_R;
exports.TEXT_MARKED_R = TEXT_MARKED_R;
exports.TEXT_PLAIN_R = TEXT_PLAIN_R;
exports.TEXT_STRIKETHROUGHED_R = TEXT_STRIKETHROUGHED_R;
exports.TRIM_STARTING_NEWLINES = TRIM_STARTING_NEWLINES;
exports.UNESCAPE_R = UNESCAPE_R;
exports.UNORDERED = UNORDERED;
exports.UNORDERED_LIST_BULLET = UNORDERED_LIST_BULLET;
exports.UNORDERED_LIST_ITEM_PREFIX = UNORDERED_LIST_ITEM_PREFIX;
exports.UNORDERED_LIST_ITEM_PREFIX_R = UNORDERED_LIST_ITEM_PREFIX_R;
exports.UNORDERED_LIST_ITEM_R = UNORDERED_LIST_ITEM_R;
exports.UNORDERED_LIST_R = UNORDERED_LIST_R;
exports.generateListItemPrefix = generateListItemPrefix;
exports.generateListItemPrefixRegex = generateListItemPrefixRegex;
exports.generateListItemRegex = generateListItemRegex;
exports.generateListRegex = generateListRegex;
//# sourceMappingURL=constants.cjs.map