@gmod-workshop/steamdown
Version:
Convert Markdown to Steam BBCode
188 lines (177 loc) • 4.56 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var __async = (__this, __arguments, generator) => {
return new Promise((resolve, reject) => {
var fulfilled = (value) => {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
};
var rejected = (value) => {
try {
step(generator.throw(value));
} catch (e) {
reject(e);
}
};
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
step((generator = generator.apply(__this, __arguments)).next());
});
};
// src/index.ts
var index_exports = {};
__export(index_exports, {
Converter: () => Converter,
Renderer: () => Renderer
});
module.exports = __toCommonJS(index_exports);
// src/Renderer.ts
var import_marked = require("marked");
var Renderer = class extends import_marked.marked.Renderer {
space() {
return "";
}
code({ text }) {
return `[code]
${text}
[/code]
`;
}
blockquote({ tokens }) {
return `[quote]
${this.parser.parse(tokens).trim()}
[/quote]
`;
}
html(token) {
return token.text;
}
heading({ tokens, depth }) {
return `[h${depth}]${this.parser.parseInline(tokens)}[/h${depth}]
`;
}
hr() {
return "[hr][/hr]\n\n";
}
list({ ordered, items }) {
if (ordered) {
return `[olist]
${items.map((i) => `${this.listitem(i)}`).join("\n")}
[/olist]
`;
}
return `[list]
${items.map((i) => `${this.listitem(i)}`).join("\n")}
[/list]
`;
}
listitem({ tokens, task, checked, loose }) {
let body = "";
if (task) {
const checkbox = this.checkbox({ checked });
body += `${checkbox} `;
}
body += this.parser.parse(tokens, loose);
return ` [*] ${body}`;
}
checkbox({ checked }) {
return checked ? "\u2705" : "\u2B1C";
}
paragraph({ tokens }) {
const text = this.parser.parseInline(tokens).replace(/\n{2,}/g, "");
return `${text}
`;
}
table({ header, rows }) {
let body = "";
let cell = "";
cell += header.map((i) => `${this.tablecell(i)}`).join("\n");
body += this.tablerow({ text: cell });
cell = "";
for (const row of rows) {
cell += row.map((i) => `${this.tablecell(i)}`).join("\n");
body += this.tablerow({ text: cell });
cell = "";
}
return `[table]
${body}[/table]
`;
}
tablerow({ text }) {
return ` [tr]
${text}
[/tr]
`;
}
tablecell({ tokens, header }) {
if (header) {
return ` [th]${this.parser.parseInline(tokens)}[/th]`;
}
return ` [td]${this.parser.parseInline(tokens)}[/td]`;
}
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";
}
del({ tokens }) {
return `[strike]${this.parser.parseInline(tokens)}[/strike]`;
}
link({ href, tokens }) {
return `[url=${href}]${this.parser.parseInline(tokens)}[/url]`;
}
image({ href }) {
return `[img]${href}[/img]`;
}
text(token) {
return token.text;
}
};
// src/Converter.ts
var import_marked2 = require("marked");
var Converter = class {
constructor(options = {}) {
var _a, _b;
this.marked = (_a = options.marked) != null ? _a : new import_marked2.Marked();
this.renderer = (_b = options.renderer) != null ? _b : new Renderer();
}
/**
* Converts Markdown to Steam BBCode.
* @param markdown Markdown to convert.
*/
convert(markdown) {
return __async(this, null, function* () {
return yield this.marked.parse(markdown, { renderer: this.renderer, async: true });
});
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Converter,
Renderer
});