Skip to content

Commit

Permalink
feat: hybrid esm/cjs
Browse files Browse the repository at this point in the history
  • Loading branch information
cossssmin committed Jul 12, 2024
1 parent c20ec79 commit 04fee2a
Show file tree
Hide file tree
Showing 8 changed files with 5,253 additions and 424 deletions.
11 changes: 11 additions & 0 deletions build.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { defineBuildConfig } from 'unbuild'

export default defineBuildConfig({
rollup: {
emitCJS: true,
},
rootDir: './lib',
outDir: '../dist',
entries: ['index.js'],
declaration: true,
})
105 changes: 105 additions & 0 deletions dist/index.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
'use strict';

const posthtmlRender = require('posthtml-render');
const posthtmlParser = require('posthtml-parser');
const MarkdownIt = require('markdown-it');
const minIndent = require('min-indent');
const node_path = require('node:path');
const node_fs = require('node:fs');
const api_js = require('posthtml/lib/api.js');

function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }

const MarkdownIt__default = /*#__PURE__*/_interopDefaultCompat(MarkdownIt);
const minIndent__default = /*#__PURE__*/_interopDefaultCompat(minIndent);

const normalize = (data) => {
const indents = minIndent__default(data);
if (indents !== 0) {
const replaceRegex = new RegExp(`^[ \\t]{${indents}}`, "gm");
return data.replace(replaceRegex, "");
}
return data;
};
const parse = (data, parser2, hasTag, inline) => {
const parsed = inline ? parser2.renderInline(data) : parser2.render(data);
return hasTag ? `
${parsed}` : parsed;
};
const importMarkdown = (src, settings) => {
const from = node_path.resolve(settings.root, src);
const content = node_fs.readFileSync(from, settings.encoding);
return normalize(content);
};
const plugin = (options) => {
const settings = {
root: "./",
encoding: "utf8",
markdownit: {},
plugins: [],
...options
};
const md = new MarkdownIt__default(settings.markdownit);
for (const plugin2 of settings.plugins) {
if (typeof plugin2.plugin === "string") {
import(plugin2.plugin).then((loaded) => {
plugin2.plugin = loaded.default || loaded;
md.use(plugin2.plugin, plugin2.options || {});
}).catch((error) => {
throw error;
});
} else {
md.use(plugin2.plugin, plugin2.options || {});
}
}
return (tree) => {
tree.match = tree.match || api_js.match;
tree.match([
{ tag: "md" },
{ tag: "markdown" },
{ attrs: { md: "" } },
{ attrs: { markdown: "" } }
], (node) => {
let inline = false;
let content = "";
if (["md", "markdown"].includes(node.tag)) {
node.tag = false;
}
if (node.attrs) {
if (node.attrs.tag) {
node.tag = node.attrs.tag;
delete node.attrs.tag;
}
if (Object.prototype.hasOwnProperty.call(node.attrs, "inline")) {
if (node.attrs.inline === "" || node.attrs.inline) {
inline = true;
}
}
for (const attribute of ["md", "markdown"]) {
if (attribute in node.attrs) {
delete node.attrs[attribute];
}
}
const src = node.attrs.src || false;
if (src) {
content += importMarkdown(src, settings);
if (tree.messages) {
const from = node_path.resolve(settings.root, src);
tree.messages.push({
type: "dependency",
file: from
});
}
delete node.attrs.src;
}
}
content += normalize(posthtmlRender.render(node.content));
const parsed = parse(content, md, node.tag, inline);
node.content = posthtmlParser.parser(parsed);
return node;
});
return tree;
};
};

module.exports = plugin;
33 changes: 33 additions & 0 deletions dist/index.d.cts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Options, PluginWithOptions } from 'markdown-it';

type MarkdownConfig = {
/**
Path relative to which markdown files are imported.
@default './'
*/
root?: string;

/**
Encoding for imported Markdown files.
@default 'utf8'
*/
encoding?: string;

/**
Options to pass to the `markdown-it` library.
@default {}
*/
markdownit?: Options;

/**
Plugins for the `markdown-it` library.
@default []
*/
plugins?: Array<PluginWithOptions<unknown>>;
};

export type { MarkdownConfig };
33 changes: 33 additions & 0 deletions dist/index.d.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Options, PluginWithOptions } from 'markdown-it';

type MarkdownConfig = {
/**
Path relative to which markdown files are imported.
@default './'
*/
root?: string;

/**
Encoding for imported Markdown files.
@default 'utf8'
*/
encoding?: string;

/**
Options to pass to the `markdown-it` library.
@default {}
*/
markdownit?: Options;

/**
Plugins for the `markdown-it` library.
@default []
*/
plugins?: Array<PluginWithOptions<unknown>>;
};

export type { MarkdownConfig };
33 changes: 33 additions & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Options, PluginWithOptions } from 'markdown-it';

type MarkdownConfig = {
/**
Path relative to which markdown files are imported.
@default './'
*/
root?: string;

/**
Encoding for imported Markdown files.
@default 'utf8'
*/
encoding?: string;

/**
Options to pass to the `markdown-it` library.
@default {}
*/
markdownit?: Options;

/**
Plugins for the `markdown-it` library.
@default []
*/
plugins?: Array<PluginWithOptions<unknown>>;
};

export type { MarkdownConfig };
98 changes: 98 additions & 0 deletions dist/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { render } from 'posthtml-render';
import { parser } from 'posthtml-parser';
import MarkdownIt from 'markdown-it';
import minIndent from 'min-indent';
import { resolve } from 'node:path';
import { readFileSync } from 'node:fs';
import { match } from 'posthtml/lib/api.js';

const normalize = (data) => {
const indents = minIndent(data);
if (indents !== 0) {
const replaceRegex = new RegExp(`^[ \\t]{${indents}}`, "gm");
return data.replace(replaceRegex, "");
}
return data;
};
const parse = (data, parser2, hasTag, inline) => {
const parsed = inline ? parser2.renderInline(data) : parser2.render(data);
return hasTag ? `
${parsed}` : parsed;
};
const importMarkdown = (src, settings) => {
const from = resolve(settings.root, src);
const content = readFileSync(from, settings.encoding);
return normalize(content);
};
const plugin = (options) => {
const settings = {
root: "./",
encoding: "utf8",
markdownit: {},
plugins: [],
...options
};
const md = new MarkdownIt(settings.markdownit);
for (const plugin2 of settings.plugins) {
if (typeof plugin2.plugin === "string") {
import(plugin2.plugin).then((loaded) => {
plugin2.plugin = loaded.default || loaded;
md.use(plugin2.plugin, plugin2.options || {});
}).catch((error) => {
throw error;
});
} else {
md.use(plugin2.plugin, plugin2.options || {});
}
}
return (tree) => {
tree.match = tree.match || match;
tree.match([
{ tag: "md" },
{ tag: "markdown" },
{ attrs: { md: "" } },
{ attrs: { markdown: "" } }
], (node) => {
let inline = false;
let content = "";
if (["md", "markdown"].includes(node.tag)) {
node.tag = false;
}
if (node.attrs) {
if (node.attrs.tag) {
node.tag = node.attrs.tag;
delete node.attrs.tag;
}
if (Object.prototype.hasOwnProperty.call(node.attrs, "inline")) {
if (node.attrs.inline === "" || node.attrs.inline) {
inline = true;
}
}
for (const attribute of ["md", "markdown"]) {
if (attribute in node.attrs) {
delete node.attrs[attribute];
}
}
const src = node.attrs.src || false;
if (src) {
content += importMarkdown(src, settings);
if (tree.messages) {
const from = resolve(settings.root, src);
tree.messages.push({
type: "dependency",
file: from
});
}
delete node.attrs.src;
}
}
content += normalize(render(node.content));
const parsed = parse(content, md, node.tag, inline);
node.content = parser(parsed);
return node;
});
return tree;
};
};

export { plugin as default };
Loading

0 comments on commit 04fee2a

Please sign in to comment.