@synstack/markdown
Version:
Opinionated Markdown utilities
260 lines (258 loc) • 6.66 kB
JavaScript
var __defProp = Object.defineProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
// 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
import { yaml } from "@synstack/yaml";
import rehypeParse from "rehype-parse";
import rehypeRemark from "rehype-remark";
import remarkFrontmatter from "remark-frontmatter";
import remarkGfm from "remark-gfm";
import remarkParse from "remark-parse";
import remarkStringify from "remark-stringify";
import { unified } from "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 unified().use(rehypeParse).use(rehypeRemark).use(remarkGfm, minifiedConfig.gfm).use(remarkStringify, 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 yaml.deserialize(header, { schema });
};
var setHeaderData = (text, data, options = {}) => {
return `---
${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 unified().use(remarkParse).use(remarkGfm, minifiedConfig.gfm).use(remarkFrontmatter, ["yaml"]).use(remarkStringify, minifiedConfig.stringify).processSync(md).toString().trim();
};
var beautify = (md) => {
return unified().use(remarkParse).use(remarkGfm, beautifiedConfig.gfm).use(remarkFrontmatter, ["yaml"]).use(remarkStringify, 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 ? `---
${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}`;
}
};
export {
MdDoc,
beautify,
fromHtml,
getBody,
getHeaderData,
markdown_bundle_exports as md,
minify,
setBody,
setHeaderData
};
//# sourceMappingURL=markdown.index.js.map