// ____ _ ____
// / ___|_ __ __ _ ___| | _| _ \ _____ ___ __
// | | | '__/ _` |/ __| |/ / | | |/ _ \ \ /\ / / '_ \
// | |___| | | (_| | (__| <| |_| | (_) \ V V /| | | |
// \____|_| \__,_|\___|_|\_\____/ \___/ \_/\_/ |_| |_|
//
//
// _ __ __ _ _ __ ___ ___ _ __
// | '_ \ / _` | '__/ __|/ _ \ '__|
// | |_) | (_| | | \__ \ __/ |
// | .__/ \__,_|_| |___/\___|_|
// |_|
const capture = "([a-z0-9:()-](?:.*[a-zA-Z:().!-])?)";
const skipSpace = "\\s*";
const htmlEscapeRegex = /[&<>"'`]/g; // used to escape html characters
/**
* @type {Array.string[]}
*
* The order of the elements of this array matters.
*/
const opcodes = [
["(^|\\n)=", "($|\\n)", "$1
$2
$3"], // lines with large headline begins with =
["(^|\\n)==", "($|\\n)", "$1$2
$3"], // lines with sub-headline begins with ==
["---", "---", "$1"], // ---trike through---
["___", "___", "$1"], // ___underline___
["(?:[,]{3})", "(?:[,]{3})", "$1"], // ,,,undercurl,,,
["(?:[(]{2})", "(?:[)]{2})", "$1"], // ((faint text))
["_", "_", "$1"], // _italic_
["\\*", "\\*", "$1"], // *bold*
["\\[\\[([a-zA-Z0-9_ ]+)\\[\\[", "\\]\\]", "$2"], // [[custom_class[[text with custom class]]
];
/** @type{Array.Array.} */
const regexes = [];
//
// Pre-compile all regexes
for (const [left, right, replacement] of opcodes) {
regexes.push([new RegExp(left + skipSpace + capture + skipSpace + right, "gi"), replacement]);
}
/** @param {string} text */
export function crackdown(text) {
text.replace(htmlEscapeRegex, (c) => {
switch (c) {
case "&":
return "&";
case "<":
return "<";
case ">":
return ">";
case '"':
return """;
case "'":
return "'";
case "`":
return "`";
default:
return c;
}
});
for (const k in regexes) {
const [regex, replacement] = regexes[k];
text = text.replace(regex, replacement);
}
console.debug("crack output", text);
return text;
}