UNPKG

@discord-user-card/markdown

Version:

A markdown parser for Discord

88 lines 2.91 kB
import { defaultRules, outputFor, parserFor } from "simple-markdown"; import { autolink } from "./rules/autolink.js"; import { blockQuote } from "./rules/blockQuote.js"; import { br } from "./rules/br.js"; import { channel } from "./rules/channel.js"; import { codeBlock } from "./rules/codeBlock.js"; import { em } from "./rules/em.js"; import { emoji } from "./rules/emoji.js"; import { emoticon } from "./rules/emoticon.js"; import { escape } from "./rules/escape.js"; import { everyone } from "./rules/everyone.js"; import { heading } from "./rules/heading.js"; import { here } from "./rules/here.js"; import { inlineCode } from "./rules/inlineCode.js"; import { link } from "./rules/link.js"; import { list } from "./rules/list.js"; import { newline } from "./rules/newline.js"; import { role } from "./rules/role.js"; import { spoiler } from "./rules/spoiler.js"; import { strikethrough } from "./rules/strikethrough.js"; import { strong } from "./rules/strong.js"; import { text } from "./rules/text.js"; import { timestamp } from "./rules/timestamp.js"; import { twemoji } from "./rules/twemoji.js"; import { underline } from "./rules/underline.js"; import { url } from "./rules/url.js"; import { user } from "./rules/user.js"; export const rules = { autolink, blockQuote, br, channel, codeBlock, em, emoji, emoticon, escape, everyone, heading, here, inlineCode, link, list, newline, role, spoiler, strikethrough, strong, text, timestamp, twemoji, underline, url, user, Array: { html: defaultRules.Array.html, rerenderInterval(arr, output, state) { const result = []; // map output over the ast, except group any text // nodes together into a single string output. for (let i = 0; i < arr.length; i++) { let node = arr[i]; if (node.type === "text") { node = { type: "text", content: node.content }; for (; i + 1 < arr.length && arr[i + 1].type === "text"; i++) { node.content += arr[i + 1].content; } } if (rules[node.type]?.rerenderInterval) result.push(output(node, state)); } const nonUndefined = result.filter((x) => x !== undefined); if (nonUndefined.length === 0) return undefined; return Math.min(...nonUndefined); }, }, }; export function parseMarkdown(markdown) { return parserFor(rules)(markdown, { inline: true }); } export function toHTML(markdown) { return outputFor(rules, "html")(parseMarkdown(markdown)); } export function rerenderInterval(markdown) { return outputFor(rules, "rerenderInterval")(parseMarkdown(markdown)); } //# sourceMappingURL=index.js.map