generated from posthtml/posthtml-plugin-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
5,253 additions
and
424 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
Oops, something went wrong.