UNPKG

@fantinodavide/discord-html-transcripts

Version:

A nicely formatted html transcript generator for discord.js.

75 lines 3.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.discordInlinePlugin = void 0; const discord_js_1 = require("discord.js"); const rendererMap = { strong: 'discord-bold', em: 'discord-italic', u: 'discord-underline', s: 'discord-strikethrough', code: 'discord-inline-code', }; /* ------------------------------------------------- 1) Mentions <@id> <@&id> <#id> ---------------------------------------------------*/ const mentionRule = (state, silent) => { const start = state.pos; if (state.src.charCodeAt(start) !== 0x3c) return false; // “<” const match = state.src.slice(start).match(/^<(@!?|@&|#)(\d+)>/); if (!match) return false; if (silent) return true; const token = state.push('discord_mention', '', 0); token.attrSet('kind', match[1] === '#' ? 'channel' : match[1].includes('&') ? 'role' : 'user'); token.attrSet('id', match[2]); state.pos += match[0].length; return true; }; /* ------------------------------------------------- 2) Plugin factory ---------------------------------------------------*/ const discordInlinePlugin = (md, opts) => { const ctx = opts; /* --- parsers --- */ md.inline.ruler.before('link', 'discord_mention', mentionRule); /* --- renderers --- */ Object.entries(rendererMap).forEach(([tag, ruleName]) => { md.renderer.rules[tag + '_open'] = () => `<${ruleName}>`; md.renderer.rules[tag + '_close'] = () => `</${ruleName}>`; }); md.renderer.rules.discord_mention = (tokens, idx) => { var _a, _b, _c, _d, _e, _f, _g, _h; const kind = tokens[idx].attrGet('kind'); const id = tokens[idx].attrGet('id'); let resolvedName = id; if (kind === 'channel') { const c = (_b = (_a = ctx.profiles) === null || _a === void 0 ? void 0 : _a._channels) === null || _b === void 0 ? void 0 : _b[id]; const isThread = c && [discord_js_1.ChannelType.PrivateThread, discord_js_1.ChannelType.PublicThread, discord_js_1.ChannelType.AnnouncementThread].includes(c.type); resolvedName = (_c = c === null || c === void 0 ? void 0 : c.name) !== null && _c !== void 0 ? _c : id; return `<discord-mention type="${isThread ? 'thread' : 'channel'}" id="${resolvedName}"></discord-mention>`; } if (kind === 'role') { const r = (_e = (_d = ctx.profiles) === null || _d === void 0 ? void 0 : _d._roles) === null || _e === void 0 ? void 0 : _e[id]; resolvedName = (_f = r === null || r === void 0 ? void 0 : r.name) !== null && _f !== void 0 ? _f : id; if ((r === null || r === void 0 ? void 0 : r.color) == '#000000') r.color = null; return `<discord-mention type="role" id="${resolvedName}" color="${r === null || r === void 0 ? void 0 : r.color}"></discord-mention>`; } // user const u = (_g = ctx.profiles) === null || _g === void 0 ? void 0 : _g[id]; resolvedName = (_h = u === null || u === void 0 ? void 0 : u.author) !== null && _h !== void 0 ? _h : `User ${id}`; return `<discord-mention type="user" id="${resolvedName}"></discord-mention>`; }; /* ... existing renderers ... */ md.renderer.rules.fence = (tokens, idx) => { const { content, info } = tokens[idx]; return `<discord-code-block language="${info.trim()}">${content}</discord-code-block>`; }; md.renderer.rules.code_inline = (tokens, idx) => `<discord-inline-code>${tokens[idx].content}</discord-inline-code>`; md.renderer.rules.link_open = (tokens, idx) => `<a href="${tokens[idx].attrGet('href')}" target="_blank" rel="noreferrer">`; md.renderer.rules.link_close = () => '</a>'; }; exports.discordInlinePlugin = discordInlinePlugin; //# sourceMappingURL=markdown-it-discord-inline.js.map