Update Marked dependency

This commit is contained in:
Bad Manners 2024-08-14 01:51:57 -03:00
parent cd67f6a5c5
commit dafb240517
4 changed files with 124 additions and 56 deletions

View file

@ -1,33 +1,70 @@
import { Marked, type RendererApi } from "marked";
import { Marked, type RendererObject } from "marked";
import { decode } from "tiny-decode";
const renderer: RendererApi = {
strong: (text) => `[b]${text}[/b]`,
em: (text) => `[i]${text}[/i]`,
del: (text) => `[s]${text}[/s]`,
codespan: (code) => code,
br: () => `\n\n`,
link: (href, _, text) => `[url=${href}]${text}[/url]`,
image: (href) => `[img]${href}[/img]`,
text: (text) => text,
paragraph: (text) => `\n${text}\n`,
list: (body, ordered) => (ordered ? `\n[ol]\n${body}[/ol]\n` : `\n[ul]\n${body}[/ul]\n`),
listitem: (text) => `[li]${text}[/li]\n`,
blockquote: (quote) => `\n[quote]${quote}[/quote]\n`,
code: (code) => `\n[code]${code}[/code]\n`,
heading: (heading) => `\n${heading}\n`,
table: (header, body) => `\n[table]\n${header}${body}[/table]\n`,
tablerow: (content) => `[tr]\n${content}[/tr]\n`,
tablecell: (content, { header }) => (header ? `[th]${content}[/th]\n` : `[td]${content}[/td]\n`),
hr: () => `\n===\n`,
html: () => {
const renderer: RendererObject = {
code({ text }) {
return `\n[code]${text}[/code]\n`;
},
blockquote({ tokens }) {
return `\n[quote]${this.parser.parseInline(tokens)}[/quote]\n`;
},
html() {
throw new Error("Not supported by BBCode: html");
},
checkbox: () => {
heading({ tokens }) {
return `\n${this.parser.parseInline(tokens)}\n`;
},
hr() {
return `\n===\n`;
},
list({ ordered, items }) {
const tag = ordered ? "ol" : "ul";
return `\n[${tag}]\n${items.map(this.listitem).join("\n")}[/${tag}]\n`;
},
listitem({ tokens }) {
return `[li]${this.parser.parseInline(tokens)}[/li]\n`;
},
checkbox() {
throw new Error("Not supported by BBCode: checkbox");
},
paragraph({ tokens }) {
return `\n${this.parser.parseInline(tokens)}\n`;
},
table({ header, rows }) {
return `\n[table]\n${this.tablerow({ text: header.map(this.tablecell).join("\n") })}\n${rows
.map((row) => this.tablerow({ text: row.map(this.tablecell).join("\n") }))
.join("\n")}[/table]\n`;
},
tablerow({ text }) {
return `[tr]\n${text}[/tr]\n`;
},
tablecell({ header, tokens }) {
const tag = header ? "th" : "td";
return `[${tag}]${this.parser.parseInline(tokens)}[/${tag}]\n`;
},
strong({ tokens }) {
return `[b]${this.parser.parseInline(tokens)}[/b]`;
},
em({ tokens }) {
return `[i]${this.parser.parseInline(tokens)}[/i]`;
},
codespan({ text }) {
return text;
},
br() {
return "\n\n";
},
del({ tokens }) {
return `[s]${this.parser.parseInline(tokens)}[/s]`;
},
link({ href, tokens }) {
return `[url=${href}]${this.parser.parseInline(tokens)}[/url]`;
},
image({ href }) {
return `[img]${href}[/url]`;
},
};
const bbcodeRenderer = new Marked({ renderer, async: false });
export const markdownToBbcode = (text: string) => decode((bbcodeRenderer.parse(text) as string).trim());
export const markdownToBbcode = (text: string) => decode(bbcodeRenderer.parse(text, { async: false }).trim());

View file

@ -1,37 +1,68 @@
import { Marked, type RendererApi } from "marked";
import { Marked, type RendererObject } from "marked";
import { decode } from "tiny-decode";
const renderer: RendererApi = {
strong: (text) => text,
em: (text) => `_${text}_`,
codespan: (code) => code,
br: () => `\n\n`,
link: (_href, _title, text) => text,
text: (text) => text,
paragraph: (text) => `\n${text}\n`,
list: (body) => `\n${body}\n`,
listitem: (text) => `- ${text}\n`,
blockquote: (quote) => `\n> ${quote}\n`,
code: (code) => `\n${code}\n`,
heading: (heading) => `\n== ${heading} ==\n`,
table: (header, body) => `\n${header}\n---\n${body}\n`,
tablerow: (content) => `${content.slice(0, -3)}\n`,
tablecell: (content) => `${content} | `,
hr: () => `\n***\n`,
del: () => {
throw new Error("Not supported by plaintext: del");
const renderer: RendererObject = {
code({ text }) {
return `\n${text}\n`;
},
image: () => {
throw new Error("Not supported by plaintext: img");
blockquote({ tokens }) {
return `\n> ${this.parser.parseInline(tokens)}\n`;
},
html: () => {
html() {
throw new Error("Not supported by plaintext: html");
},
checkbox: () => {
heading({ tokens }) {
return `\n== ${this.parser.parseInline(tokens)} ==\n`;
},
hr() {
return `\n***\n`;
},
list({ items }) {
return `\n${items.map(this.listitem).join("\n")}\n`;
},
listitem({ tokens }) {
return `- ${this.parser.parseInline(tokens)}\n`;
},
checkbox() {
throw new Error("Not supported by plaintext: checkbox");
},
paragraph({ tokens }) {
return `\n${this.parser.parseInline(tokens)}\n`;
},
table({ header, rows }) {
return `\n${this.tablerow({ text: header.map(this.tablecell).join(" | ") })}\n---\n${rows
.map((row) => this.tablerow({ text: row.map(this.tablecell).join(" | ") }))
.join("\n")}\n`;
},
tablerow({ text }) {
return text;
},
tablecell({ tokens }) {
return this.parser.parseInline(tokens);
},
strong({ tokens }) {
return this.parser.parseInline(tokens);
},
em({ tokens }) {
return `_${this.parser.parseInline(tokens)}_`;
},
codespan({ text }) {
return text;
},
br() {
return "\n\n";
},
del() {
throw new Error("Not supported by plaintext: del");
},
link({ tokens }) {
return this.parser.parseInline(tokens);
},
image() {
throw new Error("Not supported by plaintext: img");
},
};
const plaintextRenderer = new Marked({ renderer, async: false });
export const markdownToPlaintext = (text: string) => decode((plaintextRenderer.parse(text) as string).trim());
export const markdownToPlaintext = (text: string) => decode(plaintextRenderer.parse(text, { async: false }).trim());