UNPKG

@kenchan0130/markdown-to-atlassian-wiki-markup

Version:
203 lines 7.41 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AtlassianWikiMarkupRenderer = exports.CodeBlockTheme = void 0; const marked_1 = require("marked"); const language_1 = require("./language"); const utils_1 = require("./utils"); exports.CodeBlockTheme = { DJango: "DJango", Emacs: "Emacs", FadeToGrey: "FadeToGrey", Midnight: "Midnight", RDark: "RDark", Eclipse: "Eclipse", Confluence: "Confluence", }; const ListHeadCharacter = { Numbered: "#", Bullet: "*", }; const TableCellTypeCharacter = { Header: "||", NonHeader: "|", }; const confluenceListRegExp = new RegExp(`^(${Object.values(ListHeadCharacter).map(utils_1.escapeStringRegexp).join("|")})`); const unescapeHtmlSpecialCharacteres = (text) => { return text.replace(/&(#(?:\d+)|(?:#x[0-9A-Fa-f]+)|(?:\w+));?/gi, (substring, matchedString) => { const lowered = matchedString.toLowerCase(); if (lowered === "colon") { return ":"; } if (lowered === "amp") { return "&"; } if (lowered === "lt") { return "<"; } if (lowered === "gt") { return ">"; } if (lowered === "quot") { return '"'; } if (lowered.charAt(0) === "#" && lowered.charAt(1) === "x") { return String.fromCharCode(parseInt(lowered.substring(2), 16)); } if (lowered.charAt(0) === "#" && lowered.charAt(1) !== "x") { return String.fromCharCode(Number(lowered.substring(1))); } return substring; }); }; class AtlassianWikiMarkupRenderer extends marked_1.Renderer { constructor(rendererOptions) { super(); this.rendererOptions = rendererOptions; } paragraph(text) { const unescapedText = unescapeHtmlSpecialCharacteres(text); return `${unescapedText}\n\n`; } heading(text, level, _raw, _slugger) { return `h${level}. ${text}\n\n`; } strong(text) { return `*${text}*`; } em(text) { return `_${text}_`; } del(text) { return `-${text}-`; } codespan(text) { return `{{${text}}}`; } blockquote(quote) { return `{quote}${quote.trim()}{quote}`; } br() { return "\n"; } hr() { return "----\n"; } link(href, title, text) { const linkAlias = text === "" ? title : text; return linkAlias ? `[${linkAlias}|${href}]` : `[${href}]`; } list(body, ordered, _start) { const lines = body .trim() .split("\n") .filter((line) => !!line); const type = ordered ? ListHeadCharacter.Numbered : ListHeadCharacter.Bullet; const joinedLine = lines .map((line) => { return line.match(confluenceListRegExp) ? `${type}${line}` : `${type} ${line}`; }) .join("\n"); return `\n${joinedLine}\n\n`; } listitem(body) { return `${body}\n`; } checkbox(_checked) { // Confluence wiki does not support checkbox. return ""; } image(href, title, text) { const params = { alt: text, title: title, }; const paramsString = Object.entries(params) .filter(([, value]) => { return value !== null && value.trim() !== ""; }) // Sort by key to prevent the order from changing in the way of defining params .sort((a, b) => (a[0] > b[0] ? 1 : -1)) .map(([key, value]) => `${key}=${value}`) .join(","); return paramsString === "" ? `!${href}!` : `!${href}|${paramsString}!`; } table(header, body) { const tableContent = `${header}${body}`.trim(); return `\n${tableContent}\n`; } tablerow(content) { const removedEscapePipe = content.replace("\\|", ""); const twoPipeMatch = removedEscapePipe.match(/\|\|(?!.*\|\|)/); const onePipeMatch = removedEscapePipe.match(/\|(?!.*\|)/); const rowCloseType = (() => { if (!(onePipeMatch === null || onePipeMatch === void 0 ? void 0 : onePipeMatch.index)) { throw new Error("The table row expects at least one '|' in the table cell."); } if (twoPipeMatch === null || twoPipeMatch === void 0 ? void 0 : twoPipeMatch.index) { const indexDiff = onePipeMatch.index - twoPipeMatch.index; return indexDiff === 1 ? TableCellTypeCharacter.Header : TableCellTypeCharacter.NonHeader; } return TableCellTypeCharacter.NonHeader; })(); return `${content}${rowCloseType}\n`; } tablecell(content, flags) { const type = flags.header ? TableCellTypeCharacter.Header : TableCellTypeCharacter.NonHeader; const emptyComplementedContent = content === "" ? "\u{0020}" : content; return `${type}${emptyComplementedContent}`; } code(code, language, _isEscaped) { const theme = (this.rendererOptions && this.rendererOptions.codeBlock && this.rendererOptions.codeBlock.theme) || exports.CodeBlockTheme.Confluence; const usingLang = language ? language_1.markdownToWikiMarkupLanguageMapping.get(language.toLowerCase()) || language_1.AtlassianSupportLanguage.None : language_1.AtlassianSupportLanguage.None; const isDisplayLinenumbers = (() => { var _a, _b; const defaultValue = false; if (!((_a = this.rendererOptions) === null || _a === void 0 ? void 0 : _a.codeBlock)) { return defaultValue; } if (this.rendererOptions.codeBlock.showLineNumbers instanceof Function) { return this.rendererOptions.codeBlock.showLineNumbers(code, usingLang); } return (_b = this.rendererOptions.codeBlock.showLineNumbers) !== null && _b !== void 0 ? _b : defaultValue; })(); const isCollapseCodeBlock = (() => { var _a, _b; const defaultValue = false; if (!((_a = this.rendererOptions) === null || _a === void 0 ? void 0 : _a.codeBlock)) { return defaultValue; } if (this.rendererOptions.codeBlock.collapse instanceof Function) { return this.rendererOptions.codeBlock.collapse(code, usingLang); } return (_b = this.rendererOptions.codeBlock.collapse) !== null && _b !== void 0 ? _b : defaultValue; })(); const params = { language: usingLang, theme: theme, linenumbers: isDisplayLinenumbers, collapse: isCollapseCodeBlock, }; const paramsString = Object.entries(params) // Sort by key to prevent the order from changing in the way of defining params .sort((a, b) => (a[0] > b[0] ? 1 : -1)) .map(([key, value]) => `${key}=${value}`) .join("|"); return `{code:${paramsString}}\n${code}\n{code}\n\n`; } } exports.AtlassianWikiMarkupRenderer = AtlassianWikiMarkupRenderer; //# sourceMappingURL=atlassianWikiMarkupRenderer.js.map