@synstack/markdown
Version:
Opinionated Markdown utilities
299 lines (296 loc) • 8.8 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/markdown.index.ts
var markdown_index_exports = {};
__export(markdown_index_exports, {
MdDoc: () => MdDoc,
beautify: () => beautify,
fromHtml: () => fromHtml,
getBody: () => getBody,
getHeaderData: () => getHeaderData,
md: () => markdown_bundle_exports,
minify: () => minify,
setBody: () => setBody,
setHeaderData: () => setHeaderData
});
module.exports = __toCommonJS(markdown_index_exports);
// src/markdown.bundle.ts
var markdown_bundle_exports = {};
__export(markdown_bundle_exports, {
beautify: () => beautify,
fromHtml: () => fromHtml,
getBody: () => getBody,
getHeaderData: () => getHeaderData,
minify: () => minify,
setBody: () => setBody,
setHeaderData: () => setHeaderData
});
// src/markdown.lib.ts
var import_yaml = require("@synstack/yaml");
var import_rehype_parse = __toESM(require("rehype-parse"), 1);
var import_rehype_remark = __toESM(require("rehype-remark"), 1);
var import_remark_frontmatter = __toESM(require("remark-frontmatter"), 1);
var import_remark_gfm = __toESM(require("remark-gfm"), 1);
var import_remark_parse = __toESM(require("remark-parse"), 1);
var import_remark_stringify = __toESM(require("remark-stringify"), 1);
var import_unified = require("unified");
var beautifiedConfig = {
gfm: {
firstLineBlank: true,
singleTilde: false,
tableCellPadding: true,
tablePipeAlign: true
},
stringify: {
bullet: "*",
bulletOther: "-",
bulletOrdered: ".",
listItemIndent: "one",
fence: "`",
fences: true,
rule: "-",
ruleRepetition: 3,
ruleSpaces: false,
closeAtx: false,
emphasis: "_",
strong: "_",
setext: false,
quote: '"',
resourceLink: true,
tightDefinitions: false
}
};
var minifiedConfig = {
gfm: {
firstLineBlank: false,
singleTilde: false,
tableCellPadding: false,
tablePipeAlign: false
},
stringify: {
bullet: "*",
bulletOther: "-",
bulletOrdered: ".",
listItemIndent: "one",
fence: "`",
fences: true,
rule: "-",
ruleRepetition: 3,
ruleSpaces: false,
closeAtx: false,
emphasis: "_",
strong: "_",
setext: false,
quote: '"',
resourceLink: false,
tightDefinitions: true
}
};
var fromHtml = (html) => {
return (0, import_unified.unified)().use(import_rehype_parse.default).use(import_rehype_remark.default).use(import_remark_gfm.default, minifiedConfig.gfm).use(import_remark_stringify.default, minifiedConfig.stringify).processSync(html.toString()).toString().trim();
};
var HEADER_REGEX = /^---\n([\s\S]*?)\n---\n?/;
var getHeaderData = (text, { schema } = {}) => {
const header = text.toString().match(HEADER_REGEX)?.[1];
if (!header) return void 0;
return import_yaml.yaml.deserialize(header, { schema });
};
var setHeaderData = (text, data, options = {}) => {
return `---
${import_yaml.yaml.serialize(data, { schema: options.schema })}---
${getBody(text.toString())}`;
};
var getBody = (text) => {
return text.replace(HEADER_REGEX, "");
};
var setBody = (text, body) => {
const header = text.match(HEADER_REGEX)?.[0];
return `${header ?? ""}${body}`;
};
var minify = (md) => {
return (0, import_unified.unified)().use(import_remark_parse.default).use(import_remark_gfm.default, minifiedConfig.gfm).use(import_remark_frontmatter.default, ["yaml"]).use(import_remark_stringify.default, minifiedConfig.stringify).processSync(md).toString().trim();
};
var beautify = (md) => {
return (0, import_unified.unified)().use(import_remark_parse.default).use(import_remark_gfm.default, beautifiedConfig.gfm).use(import_remark_frontmatter.default, ["yaml"]).use(import_remark_stringify.default, beautifiedConfig.stringify).processSync(md).toString();
};
var MdDoc = class _MdDoc {
_body;
_data;
_options;
constructor(data, body, options = {}) {
this._body = body;
this._data = data;
this._options = options ?? {};
}
/**
* Create a new markdown document with options
* @param options
* @param options.schema - The zod schema to use for serialization/deserialization (optional)
* @returns A new markdown document instance
*/
static withOptions(options) {
return new _MdDoc(
void 0,
"",
options
);
}
/**
* Create a new markdown document from a string
* @param text - The markdown document
* @returns The markdown document
*/
static fromString(text) {
return new _MdDoc(
getHeaderData(text),
getBody(text)
);
}
/**
* Create a new markdown document from HTML
* @param html - The HTML to convert
* @returns The markdown document
*/
static fromHtml(html) {
return new _MdDoc(void 0, fromHtml(html));
}
/**
* Get the body of the markdown document
* @returns The body of the markdown document
*/
get body() {
return this._body;
}
/**
* Get the data of the markdown document
* @returns The data of the markdown document
*/
get data() {
return this._data;
}
/**
* Get the header of the markdown document
* @returns The header of the markdown document
*/
get header() {
return this._data ? `---
${import_yaml.yaml.serialize(this._data)}---
` : "";
}
/**
* Get the options of the markdown document
* @returns The options of the markdown document
*/
get options() {
return this._options;
}
/**
* Create a new markdown document from a string
* @param text - The markdown document
* @returns A new markdown document
*/
fromString(text) {
const validatedData = getHeaderData(text, {
schema: this._options.schema
});
return new _MdDoc(
validatedData,
getBody(text),
this._options
);
}
/**
* Create a new markdown document from HTML
* @param html - The HTML to convert
* @returns A new markdown document
*/
fromHtml(html) {
return new _MdDoc(this._data, fromHtml(html), this._options);
}
/**
* Set the data of the markdown document
* @param data - The data to set
* @returns A new markdown document
*/
setData(data) {
const validatedData = this._options.schema ? this._options.schema.parse(data) : data;
return new _MdDoc(validatedData, this._body, this._options);
}
/**
* Set the body of the markdown document
* @param text - The body to set
* @returns A new markdown document
*/
setBody(text) {
return new _MdDoc(this._data, text, this._options);
}
/**
* Minify the markdown document for better LLM processing
* @returns A new markdown document
*/
minify() {
return new _MdDoc(this._data, minify(this.body), this._options);
}
/**
* Beautify the markdown document for better human readability
* @returns A new markdown document
*/
beautify() {
return new _MdDoc(this._data, beautify(this.body), this._options);
}
/**
* Get the markdown document as a string
*
* @alias {@link toString}
* @returns The markdown document as a string
*/
toMd() {
return this.toString();
}
/**
* Get the markdown document as a string
* @returns The markdown document as a string
*/
toString() {
return `${this.header}${this.body}`;
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
MdDoc,
beautify,
fromHtml,
getBody,
getHeaderData,
md,
minify,
setBody,
setHeaderData
});
//# sourceMappingURL=markdown.index.cjs.map