@isdk/mdast-plus 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.cn.md +2 -0
- package/README.md +2 -0
- package/dist/index.d.mts +22 -9
- package/dist/index.d.ts +22 -9
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/docs/README.md +2 -0
- package/docs/_media/CONTRIBUTING.md +2 -0
- package/docs/_media/README.cn.md +2 -0
- package/docs/classes/MdastBasePipeline.md +52 -20
- package/docs/classes/MdastPipeline.md +65 -29
- package/docs/enumerations/PipelineStage.md +6 -6
- package/docs/functions/astCompiler.md +1 -1
- package/docs/functions/jsonParser.md +1 -1
- package/docs/functions/mdast.md +1 -1
- package/docs/interfaces/MdastDataOrigin.md +4 -4
- package/docs/interfaces/MdastFormat.md +7 -7
- package/docs/interfaces/MdastMark.md +3 -3
- package/docs/interfaces/MdastPlugin.md +9 -9
- package/docs/interfaces/MdastSub.md +3 -3
- package/docs/interfaces/MdastSup.md +3 -3
- package/docs/interfaces/ReadabilityOptions.md +18 -10
- package/docs/type-aliases/PipelineStageName.md +1 -1
- package/docs/variables/DefaultPipelineStage.md +1 -1
- package/docs/variables/astFormat.md +1 -1
- package/docs/variables/htmlFormat.md +1 -1
- package/docs/variables/htmlReadability.md +2 -2
- package/docs/variables/htmlReadabilityPlugin.md +6 -2
- package/docs/variables/htmlReadabilityPlugins.md +2 -2
- package/docs/variables/markdownFormat.md +1 -1
- package/docs/variables/restoreReadabilityMetaPlugin.md +1 -1
- package/package.json +1 -1
package/README.cn.md
CHANGED
|
@@ -89,6 +89,8 @@ console.log(vfile.value); // 序列化后的 HTML 字符串
|
|
|
89
89
|
|
|
90
90
|
`mdast-plus` 内部使用 [unified](https://github.com/unifiedjs/unified)。如果您多次添加同一个插件函数,最后的配置将**覆盖**之前的配置。
|
|
91
91
|
|
|
92
|
+
> **警告**: 将 `false` 作为插件选项传递(例如 `.use(myPlugin, false)`)将**完全禁用**该插件。对于普通插件,这意味着它们将不会运行。对于标记为 `main: true` 的插件(如默认解析器的替代者),如果它们被 `false` 禁用,它们将**不会**替换该阶段的默认插件,从而安全地回退到默认行为。如果你需要跳过插件逻辑但保持其激活状态(例如为了保留其解析器),请改用选项对象,例如 `{ enable: false }`。
|
|
93
|
+
|
|
92
94
|
```typescript
|
|
93
95
|
// 插件将只执行一次,且选项为: 2
|
|
94
96
|
pipeline.use(myPlugin, { option: 1 });
|
package/README.md
CHANGED
|
@@ -89,6 +89,8 @@ console.log(vfile.value); // The serialized HTML string
|
|
|
89
89
|
|
|
90
90
|
`mdast-plus` uses [unified](https://github.com/unifiedjs/unified) internally. If you add the same plugin function multiple times, the last configuration **overrides** the previous ones.
|
|
91
91
|
|
|
92
|
+
> **Warning**: Passing `false` as a plugin option (e.g., `.use(myPlugin, false)`) will **disable** the plugin entirely. For regular plugins, this means they simply won't run. For plugins marked as `main: true` (like replacements for the default parser), if they are disabled with `false`, they will **not** replace the default plugin of that stage, providing a safe fallback to the default behavior. If you want to bypass a plugin's logic while keeping it active (e.g. to maintain its parser), use an options object like `{ enable: false }` instead.
|
|
93
|
+
|
|
92
94
|
```typescript
|
|
93
95
|
// The plugin will run ONCE with option: 2
|
|
94
96
|
pipeline.use(myPlugin, { option: 1 });
|
package/dist/index.d.mts
CHANGED
|
@@ -1598,14 +1598,14 @@ declare class MdastBasePipeline {
|
|
|
1598
1598
|
* @param options - Arguments for the plugin(s).
|
|
1599
1599
|
* @returns The pipeline instance for chaining.
|
|
1600
1600
|
*/
|
|
1601
|
-
useAt(stage: PipelineStageName, plugin: Plugin | MdastPlugin | (Plugin | MdastPlugin)[], ...options: any[]): this;
|
|
1601
|
+
useAt(stage: PipelineStage | PipelineStageName, plugin: Plugin | MdastPlugin | (Plugin | MdastPlugin)[], ...options: any[]): this;
|
|
1602
1602
|
/**
|
|
1603
1603
|
* Adds a plugin or an array of plugins to the pipeline. The stage is taken from the plugin object(s).
|
|
1604
1604
|
* @param plugin - The MdastPlugin object or an array of them.
|
|
1605
1605
|
* @param options - Arguments for the plugin(s) (overrides plugin.options if provided).
|
|
1606
1606
|
* @returns The pipeline instance for chaining.
|
|
1607
1607
|
*/
|
|
1608
|
-
useAt(plugin: MdastPlugin |
|
|
1608
|
+
useAt(plugin: MdastPlugin | MdastPlugin[], ...options: any[]): this;
|
|
1609
1609
|
/**
|
|
1610
1610
|
* Sets the priority order for the most recently added plugin.
|
|
1611
1611
|
* Plugins with lower order run earlier within the same stage.
|
|
@@ -1613,6 +1613,16 @@ declare class MdastBasePipeline {
|
|
|
1613
1613
|
* @returns The pipeline instance for chaining.
|
|
1614
1614
|
*/
|
|
1615
1615
|
priority(order: number): this;
|
|
1616
|
+
/**
|
|
1617
|
+
* Modifies the options of a plugin that is already in the pipeline queue.
|
|
1618
|
+
* Searches from the end of the queue and updates the first match found.
|
|
1619
|
+
*
|
|
1620
|
+
* @param pluginName - The name of the plugin to modify.
|
|
1621
|
+
* Matches against explicit plugin name or function name.
|
|
1622
|
+
* @param options - The new options to pass to the plugin (replaces existing options).
|
|
1623
|
+
* @returns The pipeline instance for chaining.
|
|
1624
|
+
*/
|
|
1625
|
+
configure(pluginName: string, ...options: any[]): this;
|
|
1616
1626
|
/**
|
|
1617
1627
|
* Assembles a unified processor based on the sorted plugin queue.
|
|
1618
1628
|
* @protected
|
|
@@ -1646,14 +1656,14 @@ declare class MdastPipeline extends MdastBasePipeline {
|
|
|
1646
1656
|
* @param options.overrides - Map for plugin option overrides.
|
|
1647
1657
|
*/
|
|
1648
1658
|
toAst(options?: {
|
|
1649
|
-
stage?: PipelineStageName;
|
|
1659
|
+
stage?: PipelineStage | PipelineStageName;
|
|
1650
1660
|
overrides?: Record<string, any>;
|
|
1651
1661
|
}): Promise<Root$1>;
|
|
1652
1662
|
/** Alias for toHtml() */
|
|
1653
1663
|
toHTML(): Promise<string>;
|
|
1654
1664
|
/** Alias for toAst() */
|
|
1655
1665
|
toAST(options?: {
|
|
1656
|
-
stage?: PipelineStageName;
|
|
1666
|
+
stage?: PipelineStage | PipelineStageName;
|
|
1657
1667
|
overrides?: Record<string, any>;
|
|
1658
1668
|
}): Promise<Root$1>;
|
|
1659
1669
|
}
|
|
@@ -1699,21 +1709,23 @@ declare function jsonParser(this: any): void;
|
|
|
1699
1709
|
declare const astFormat: MdastFormat;
|
|
1700
1710
|
|
|
1701
1711
|
interface ReadabilityOptions {
|
|
1702
|
-
|
|
1703
|
-
readability?: Record<string, any
|
|
1712
|
+
url?: string;
|
|
1713
|
+
readability?: Record<string, any> | false;
|
|
1704
1714
|
jsdom?: Record<string, any>;
|
|
1715
|
+
'rehype-parse'?: Record<string, any>;
|
|
1705
1716
|
}
|
|
1706
1717
|
/**
|
|
1707
1718
|
* A unified/rehype plugin that uses Mozilla's Readability to parse the input HTML.
|
|
1708
1719
|
*/
|
|
1709
|
-
declare const htmlReadability: Plugin<[
|
|
1720
|
+
declare const htmlReadability: Plugin<[ReadabilityOptions?], string, Root>;
|
|
1710
1721
|
/**
|
|
1711
1722
|
* Pre-configured MdastPlugin for html-readability.
|
|
1712
1723
|
*/
|
|
1713
1724
|
declare const htmlReadabilityPlugin: {
|
|
1714
1725
|
name: string;
|
|
1715
|
-
plugin: Plugin<[(
|
|
1726
|
+
plugin: Plugin<[(ReadabilityOptions | undefined)?], string, Root>;
|
|
1716
1727
|
stage: PipelineStage;
|
|
1728
|
+
main: boolean;
|
|
1717
1729
|
};
|
|
1718
1730
|
/**
|
|
1719
1731
|
* Plugin to restore readability metadata after HAST to MDAST conversion.
|
|
@@ -1729,8 +1741,9 @@ declare const restoreReadabilityMetaPlugin: {
|
|
|
1729
1741
|
*/
|
|
1730
1742
|
declare const htmlReadabilityPlugins: ({
|
|
1731
1743
|
name: string;
|
|
1732
|
-
plugin: Plugin<[(
|
|
1744
|
+
plugin: Plugin<[(ReadabilityOptions | undefined)?], string, Root>;
|
|
1733
1745
|
stage: PipelineStage;
|
|
1746
|
+
main: boolean;
|
|
1734
1747
|
} | {
|
|
1735
1748
|
name: string;
|
|
1736
1749
|
plugin: () => (tree: any, file: any) => void;
|
package/dist/index.d.ts
CHANGED
|
@@ -1598,14 +1598,14 @@ declare class MdastBasePipeline {
|
|
|
1598
1598
|
* @param options - Arguments for the plugin(s).
|
|
1599
1599
|
* @returns The pipeline instance for chaining.
|
|
1600
1600
|
*/
|
|
1601
|
-
useAt(stage: PipelineStageName, plugin: Plugin | MdastPlugin | (Plugin | MdastPlugin)[], ...options: any[]): this;
|
|
1601
|
+
useAt(stage: PipelineStage | PipelineStageName, plugin: Plugin | MdastPlugin | (Plugin | MdastPlugin)[], ...options: any[]): this;
|
|
1602
1602
|
/**
|
|
1603
1603
|
* Adds a plugin or an array of plugins to the pipeline. The stage is taken from the plugin object(s).
|
|
1604
1604
|
* @param plugin - The MdastPlugin object or an array of them.
|
|
1605
1605
|
* @param options - Arguments for the plugin(s) (overrides plugin.options if provided).
|
|
1606
1606
|
* @returns The pipeline instance for chaining.
|
|
1607
1607
|
*/
|
|
1608
|
-
useAt(plugin: MdastPlugin |
|
|
1608
|
+
useAt(plugin: MdastPlugin | MdastPlugin[], ...options: any[]): this;
|
|
1609
1609
|
/**
|
|
1610
1610
|
* Sets the priority order for the most recently added plugin.
|
|
1611
1611
|
* Plugins with lower order run earlier within the same stage.
|
|
@@ -1613,6 +1613,16 @@ declare class MdastBasePipeline {
|
|
|
1613
1613
|
* @returns The pipeline instance for chaining.
|
|
1614
1614
|
*/
|
|
1615
1615
|
priority(order: number): this;
|
|
1616
|
+
/**
|
|
1617
|
+
* Modifies the options of a plugin that is already in the pipeline queue.
|
|
1618
|
+
* Searches from the end of the queue and updates the first match found.
|
|
1619
|
+
*
|
|
1620
|
+
* @param pluginName - The name of the plugin to modify.
|
|
1621
|
+
* Matches against explicit plugin name or function name.
|
|
1622
|
+
* @param options - The new options to pass to the plugin (replaces existing options).
|
|
1623
|
+
* @returns The pipeline instance for chaining.
|
|
1624
|
+
*/
|
|
1625
|
+
configure(pluginName: string, ...options: any[]): this;
|
|
1616
1626
|
/**
|
|
1617
1627
|
* Assembles a unified processor based on the sorted plugin queue.
|
|
1618
1628
|
* @protected
|
|
@@ -1646,14 +1656,14 @@ declare class MdastPipeline extends MdastBasePipeline {
|
|
|
1646
1656
|
* @param options.overrides - Map for plugin option overrides.
|
|
1647
1657
|
*/
|
|
1648
1658
|
toAst(options?: {
|
|
1649
|
-
stage?: PipelineStageName;
|
|
1659
|
+
stage?: PipelineStage | PipelineStageName;
|
|
1650
1660
|
overrides?: Record<string, any>;
|
|
1651
1661
|
}): Promise<Root$1>;
|
|
1652
1662
|
/** Alias for toHtml() */
|
|
1653
1663
|
toHTML(): Promise<string>;
|
|
1654
1664
|
/** Alias for toAst() */
|
|
1655
1665
|
toAST(options?: {
|
|
1656
|
-
stage?: PipelineStageName;
|
|
1666
|
+
stage?: PipelineStage | PipelineStageName;
|
|
1657
1667
|
overrides?: Record<string, any>;
|
|
1658
1668
|
}): Promise<Root$1>;
|
|
1659
1669
|
}
|
|
@@ -1699,21 +1709,23 @@ declare function jsonParser(this: any): void;
|
|
|
1699
1709
|
declare const astFormat: MdastFormat;
|
|
1700
1710
|
|
|
1701
1711
|
interface ReadabilityOptions {
|
|
1702
|
-
|
|
1703
|
-
readability?: Record<string, any
|
|
1712
|
+
url?: string;
|
|
1713
|
+
readability?: Record<string, any> | false;
|
|
1704
1714
|
jsdom?: Record<string, any>;
|
|
1715
|
+
'rehype-parse'?: Record<string, any>;
|
|
1705
1716
|
}
|
|
1706
1717
|
/**
|
|
1707
1718
|
* A unified/rehype plugin that uses Mozilla's Readability to parse the input HTML.
|
|
1708
1719
|
*/
|
|
1709
|
-
declare const htmlReadability: Plugin<[
|
|
1720
|
+
declare const htmlReadability: Plugin<[ReadabilityOptions?], string, Root>;
|
|
1710
1721
|
/**
|
|
1711
1722
|
* Pre-configured MdastPlugin for html-readability.
|
|
1712
1723
|
*/
|
|
1713
1724
|
declare const htmlReadabilityPlugin: {
|
|
1714
1725
|
name: string;
|
|
1715
|
-
plugin: Plugin<[(
|
|
1726
|
+
plugin: Plugin<[(ReadabilityOptions | undefined)?], string, Root>;
|
|
1716
1727
|
stage: PipelineStage;
|
|
1728
|
+
main: boolean;
|
|
1717
1729
|
};
|
|
1718
1730
|
/**
|
|
1719
1731
|
* Plugin to restore readability metadata after HAST to MDAST conversion.
|
|
@@ -1729,8 +1741,9 @@ declare const restoreReadabilityMetaPlugin: {
|
|
|
1729
1741
|
*/
|
|
1730
1742
|
declare const htmlReadabilityPlugins: ({
|
|
1731
1743
|
name: string;
|
|
1732
|
-
plugin: Plugin<[(
|
|
1744
|
+
plugin: Plugin<[(ReadabilityOptions | undefined)?], string, Root>;
|
|
1733
1745
|
stage: PipelineStage;
|
|
1746
|
+
main: boolean;
|
|
1734
1747
|
} | {
|
|
1735
1748
|
name: string;
|
|
1736
1749
|
plugin: () => (tree: any, file: any) => void;
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var t,e=Object.create,r=Object.defineProperty,i=Object.getOwnPropertyDescriptor,n=Object.getOwnPropertyNames,s=Object.getPrototypeOf,o=Object.prototype.hasOwnProperty,a=(t,e,s,a)=>{if(e&&"object"==typeof e||"function"==typeof e)for(let u of n(e))o.call(t,u)||u===s||r(t,u,{get:()=>e[u],enumerable:!(a=i(e,u))||a.enumerable});return t},u=(t,i,n)=>(n=null!=t?e(s(t)):{},a(!i&&t&&t.__esModule?n:r(n,"default",{value:t,enumerable:!0}),t)),l={};((t,e)=>{for(var i in e)r(t,i,{get:e[i],enumerable:!0})})(l,{DefaultPipelineStage:()=>p,MdastBasePipeline:()=>Q,MdastPipeline:()=>U,PipelineStage:()=>c,astCompiler:()=>V,astFormat:()=>G,htmlFormat:()=>C,htmlReadability:()=>Y,htmlReadabilityPlugin:()=>Z,htmlReadabilityPlugins:()=>et,jsonParser:()=>_,markdownFormat:()=>R,mdast:()=>W,restoreReadabilityMetaPlugin:()=>tt}),module.exports=(t=l,a(r({},"__esModule",{value:!0}),t));var c=(t=>(t[t.parse=0]="parse",t[t.normalize=100]="normalize",t[t.compile=200]="compile",t[t.finalize=300]="finalize",t[t.stringify=400]="stringify",t))(c||{}),p=200,f=require("unified"),h=require("vfile"),m=u(require("remark-parse")),g=u(require("remark-stringify")),d=u(require("remark-gfm")),y=u(require("remark-directive")),b=u(require("remark-math")),w=u(require("remark-frontmatter")),v=require("unist-util-visit");function k(t,e){return{type:t,children:e,data:{hName:t}}}var q={mark:(t,e,r)=>"=="+r.containerPhrasing(t,{before:"==",after:"=="})+"==",sub:(t,e,r)=>"~"+r.containerPhrasing(t,{before:"~",after:"~"})+"~",sup:(t,e,r)=>"^"+r.containerPhrasing(t,{before:"^",after:"^"})+"^"},j={plugin:()=>t=>{!function(t){(0,v.visit)(t,"text",(t,e,r)=>{if(!r||void 0===e)return;const i=t.value;let n=0;const s=[];let o=!1;const a=/(==[^=]+==|~[^~]+~|\^[^^]+\^)/g;let u;for(;null!==(u=a.exec(i));){o=!0;const t=u[0],e=u.index;e>n&&s.push({type:"text",value:i.slice(n,e)});let r="mark",l="";t.startsWith("==")?(r="mark",l=t.slice(2,-2)):t.startsWith("~")?(r="sub",l=t.slice(1,-1)):t.startsWith("^")&&(r="sup",l=t.slice(1,-1)),s.push(k(r,[{type:"text",value:l}])),n=a.lastIndex}return o?(n<i.length&&s.push({type:"text",value:i.slice(n)}),r.children.splice(e,1,...s),e+s.length):void 0})}(t)},stage:100},M=require("unist-util-visit"),P={error:"danger",warn:"warning",success:"tip",important:"important",caution:"caution",note:"note"},x={plugin:()=>async t=>{(0,M.visit)(t,["containerDirective","leafDirective","textDirective"],t=>{const e=t,r=e.name.toLowerCase();if(e.name=P[r]||r,e.children&&e.children.length>0){const t=e.children[0];if(t.data?.directiveLabel||"directiveLabel"===t.type){const r=t;let i="";(0,M.visit)(r,"text",t=>{i+=t.value}),i&&!e.attributes?.title&&(e.attributes=e.attributes||{},e.attributes.title=i.trim()),e.children.shift()}}e.attributes?.title&&(e.attributes.title=String(e.attributes.title).trim()),e.data=e.data||{},e.data.hName=e.data.hName||("containerDirective"===e.type?"div":"span"),e.data.hProperties={...e.data.hProperties||{},...e.attributes,className:[e.name,e.data.hProperties?.className].filter(Boolean).join(" ")}})},stage:100,order:10},F=require("unist-util-visit"),S={plugin:()=>async t=>{(0,F.visit)(t,"tableCell",t=>{if(t.data){const{rowspan:e,colspan:r}=t.data;t.data.hProperties=t.data.hProperties||{},void 0!==e&&(t.data.hProperties.rowSpan=e,delete t.data.rowspan),void 0!==r&&(t.data.hProperties.colSpan=r,delete t.data.colspan)}})},stage:100,order:20},O=require("unist-util-visit"),A=require("shell-quote");var D={plugin:()=>async t=>{(0,O.visit)(t,"code",t=>{if(t.meta){const e=function(t){const e={},r=(0,A.parse)(t);for(const t of r)if("string"==typeof t){const r=t.split("=",2);2===r.length?e[r[0]]=r[1]:e[t]="true"}return e}(t.meta),r=t.data=t.data||{};e.title&&(r.title=e.title),e.filename&&(r.filename=e.filename),r.kv={...r.kv||{},...e}}})},stage:100,order:30},T=require("unist-util-visit"),N={plugin:()=>async t=>{(0,T.visit)(t,"image",t=>{const e=t.data=t.data||{},r=e.hProperties=e.hProperties||{},i=/[#?&](?:width=([0-9]+))?(?:&?height=([0-9]+))?(?:=([0-9]+)x([0-9]+))?$/,n=t.url.match(i);if(n){const e=n[1]||n[3],s=n[2]||n[4];e&&!r.width&&(r.width=parseInt(e,10)),s&&!r.height&&(r.height=parseInt(s,10)),t.url=t.url.replace(i,"")}e.width&&!r.width&&(r.width=e.width),e.height&&!r.height&&(r.height=e.height)})},stage:100,order:40},z=[{plugin:m.default,stage:0},{plugin:d.default,options:[{singleTilde:!1}],stage:0},{plugin:y.default,stage:0},{plugin:b.default,stage:0},{plugin:w.default,options:[["yaml","toml"]],stage:0},x,S,D,N,j],E=[{plugin:g.default,options:[{handlers:q}],stage:400},{plugin:d.default,options:[{singleTilde:!1}],stage:400},{plugin:y.default,stage:400},{plugin:b.default,stage:400},{plugin:w.default,options:[["yaml","toml"]],stage:400}];E.forEach(t=>{t.plugin===g.default?t.order=100:t.order=10});var R={id:"markdown",title:"Markdown (GFM + Directives)",extensions:["md","markdown","mdown","mkdn"],mediaTypes:["text/markdown"],input:z,output:E},H=u(require("rehype-parse")),$=u(require("rehype-remark")),B=u(require("remark-rehype")),I=u(require("rehype-sanitize")),L=u(require("rehype-stringify")),C={id:"html",title:"HTML",extensions:["html","htm"],mediaTypes:["text/html"],input:[{name:"rehype-parse",plugin:H.default,stage:0},{name:"rehype-remark",plugin:$.default,options:[{handlers:{mark:(t,e)=>{const r={type:"mark",children:t.all(e)};return t.patch(e,r),r},sub:(t,e)=>{const r={type:"sub",children:t.all(e)};return t.patch(e,r),r},sup:(t,e)=>{const r={type:"sup",children:t.all(e)};return t.patch(e,r),r}}}],stage:0}],output:[{plugin:B.default,stage:300,order:10},{plugin:I.default,options:[{...I.defaultSchema,tagNames:[...I.defaultSchema.tagNames||[],"mark","sub","sup"],attributes:{...I.defaultSchema.attributes,"*":[...I.defaultSchema.attributes?.["*"]||[],"className","id","style"],td:[...I.defaultSchema.attributes?.td||[],"rowSpan","colSpan","rowspan","colspan"],th:[...I.defaultSchema.attributes?.th||[],"rowSpan","colSpan","rowspan","colspan"],img:[...I.defaultSchema.attributes?.img||[],"width","height"]}}],stage:300,order:20},{plugin:L.default,stage:400}]};function V(){this.Compiler=t=>t}function _(){this.Parser=t=>JSON.parse(t)}var G={id:"ast",title:"MDAST",input:[{plugin:_,stage:0},x,S,D,N,j],output:[{plugin:V,options:[],stage:400}]};function J(t){return"object"==typeof t&&null!==t&&"string"==typeof t.type}var K=class t{constructor(t){this.queue=[],this._data={},this.input=t}static register(t){this.registry.set(t.id,t)}static getFormat(t){return this.registry.get(t)}data(t,e){return"string"==typeof t?this._data[t]=e:Object.assign(this._data,t),this}getFormat(t){return this.constructor.getFormat(t)}resolveFormat(t){if("string"==typeof t){const e=this.getFormat(t);if(!e)throw new Error(`[MdastPlus] Format '${t}' is not registered.`);return e}return t}toRuntimeEntry(t,e,r){let i=e;void 0!==t.stage&&(i="string"==typeof t.stage?c[t.stage]??e:t.stage);let n=t.options||[];const s=t.name||t.plugin.name;if(r&&s&&s in r){const e=r[s];"object"!=typeof e||null===e||Array.isArray(e)?n=Array.isArray(e)?e:[e]:("main"in e&&(t.main=!!e.main),"before"in e&&(t.before=e.before),"after"in e&&(t.after=e.after),n=[e])}return{name:s,plugin:t.plugin,options:n,stage:i,order:t.order||0,main:t.main,before:t.before,after:t.after}}ensureInputPlugins(e,r,i=400){const n=e.some(t=>0===(t.stage??p)),s=J(this.input);if(!n){let n=[];if(s){const e=t.getFormat("ast");e&&e.input&&(n=e.input)}else{const e=t.getFormat("markdown");e&&e.input&&(n=e.input)}for(const t of n){const n=this.toRuntimeEntry(t,0,r);(n.stage??p)<=i&&e.push(n)}}}from(t,e){const r=this.resolveFormat(t);if(!r.input||0===r.input.length)throw new Error(`[MdastPlus] Format '${r.id}' does not support input.`);for(const t of r.input)this.queue.push(this.toRuntimeEntry(t,0,e));return this}async to(t,e){const r=this.resolveFormat(t);if(!r.output)throw new Error(`[MdastPlus] Format '${r.id}' does not support output.`);const i=[...this.queue];this.ensureInputPlugins(i,e);for(const t of r.output)i.push(this.toRuntimeEntry(t,300,e));const n=this.assembleProcessor(i);if(J(this.input)){const t=await n.run(this.input),e=n.stringify(t),r=new h.VFile;return"string"==typeof e||Buffer.isBuffer(e)?r.value=e:r.result=e,r}return n.process(this.input)}use(t,...e){return this.useAt("compile",t,...e)}useAt(t,e,...r){if(Array.isArray(t)){for(const i of t)this.useAt(i,e,...r);return this}if(Array.isArray(e)){for(const i of e)this.useAt(t,i,...r);return this}if("object"==typeof t&&null!==t&&"plugin"in t){const i=t,n=void 0!==i.stage?"string"==typeof i.stage?i.stage:c[i.stage]:"compile",s=void 0!==e?[e,...r]:i.options;this.queue.push(this.toRuntimeEntry({...i,options:s},c[n]))}else{const i=t;if("object"==typeof e&&null!==e&&"plugin"in e){const t=e,n=r.length>0?r:t.options;this.queue.push(this.toRuntimeEntry({...t,options:n},c[i]))}else e&&this.queue.push({plugin:e,options:r,stage:c[i],order:0})}return this}priority(t){const e=this.queue[this.queue.length-1];return e&&(e.order=t),this}assembleProcessor(t){const e={};for(const r of t){const t=r.stage??p;e[t]||(e[t]=[]),e[t].push(r)}const r=[],i=Object.keys(e).map(Number).sort((t,e)=>t-e);for(const t of i){const i=e[t].sort((t,e)=>(t.order||0)-(e.order||0)),n=i.findIndex(t=>t.main);if(-1!==n){const t=i[n];i.splice(n,1),i[0]=t}let s=!0,o=0;for(;s&&o<i.length;){s=!1,o++;for(let t=0;t<i.length;t++){const e=i[t];if(e.after){const r=i.findIndex(t=>t.name===e.after);if(-1!==r&&r>t){i.splice(t,1),i.splice(r,0,e),s=!0;break}}if(e.before){const r=i.findIndex(t=>t.name===e.before);if(-1!==r&&r<t){i.splice(t,1),i.splice(r,0,e),s=!0;break}}}}r.push(...i)}const n=(0,f.unified)();Object.keys(this._data).length>0&&n.data(this._data);for(const t of r)n.use(t.plugin,...t.options||[]);return n}};K.registry=new Map;var Q=K,U=class extends Q{async toMarkdown(){const t=await this.to("markdown");return String(t)}toMarkdownVFile(){return this.to("markdown")}async toHtml(){const t=await this.to("html");return String(t)}toHtmlVFile(){return this.to("html")}async toAst(t){if(t?.stage){const e=c[t.stage],r=this.queue.filter(t=>(t.stage??p)<=e);r.push({plugin:V,options:[],stage:400,order:0}),this.ensureInputPlugins(r,t.overrides,e);const i=this.assembleProcessor(r);if(J(this.input)){return await i.run(this.input)}return(await i.process(this.input)).result}return(await this.to("ast",t?.overrides)).result}toHTML(){return this.toHtml()}toAST(t){return this.toAst(t)}};function W(t){return new U(t)}U.register(R),U.register(C),U.register(G);var X=require("hast-util-from-html"),Y=function(t){let e={};"boolean"==typeof t?e={enable:t}:t&&(e=t);const{enable:r,readability:i,jsdom:n}=e;!1!==r&&(this.parser=function(t,e){let r,s;try{r=require("jsdom").JSDOM;s=require("@mozilla/readability").Readability}catch(t){throw new Error("[html-readability] Dependency missing. Please install 'jsdom' and '@mozilla/readability'.")}const o=new s(new r(t,n).window.document,i).parse();if(!o||!o.content)return(0,X.fromHtml)(t,{fragment:!0});const a=(0,X.fromHtml)(o.content,{fragment:!0}),u={title:o.title,byline:o.byline,excerpt:o.excerpt,siteName:o.siteName,lang:o.lang};return e&&(e.data=e.data||{},e.data.readability=u),a&&(a.data=a.data||{},a.data.readability=u),a})},Z={name:"readability",plugin:Y,stage:0},tt={name:"restore-readability-meta",plugin:()=>(t,e)=>{e.data?.readability&&(t.data=t.data||{},t.data.readability=e.data.readability)},stage:0,after:"rehype-remark"},et=[Z,tt];
|
|
1
|
+
"use strict";var t,e=Object.create,r=Object.defineProperty,i=Object.getOwnPropertyDescriptor,n=Object.getOwnPropertyNames,s=Object.getPrototypeOf,o=Object.prototype.hasOwnProperty,a=(t,e,s,a)=>{if(e&&"object"==typeof e||"function"==typeof e)for(let u of n(e))o.call(t,u)||u===s||r(t,u,{get:()=>e[u],enumerable:!(a=i(e,u))||a.enumerable});return t},u=(t,i,n)=>(n=null!=t?e(s(t)):{},a(!i&&t&&t.__esModule?n:r(n,"default",{value:t,enumerable:!0}),t)),l={};((t,e)=>{for(var i in e)r(t,i,{get:e[i],enumerable:!0})})(l,{DefaultPipelineStage:()=>p,MdastBasePipeline:()=>Q,MdastPipeline:()=>U,PipelineStage:()=>c,astCompiler:()=>V,astFormat:()=>G,htmlFormat:()=>L,htmlReadability:()=>Y,htmlReadabilityPlugin:()=>Z,htmlReadabilityPlugins:()=>et,jsonParser:()=>_,markdownFormat:()=>R,mdast:()=>W,restoreReadabilityMetaPlugin:()=>tt}),module.exports=(t=l,a(r({},"__esModule",{value:!0}),t));var c=(t=>(t[t.parse=0]="parse",t[t.normalize=100]="normalize",t[t.compile=200]="compile",t[t.finalize=300]="finalize",t[t.stringify=400]="stringify",t))(c||{}),p=200,f=require("unified"),h=require("vfile"),g=u(require("remark-parse")),m=u(require("remark-stringify")),d=u(require("remark-gfm")),y=u(require("remark-directive")),b=u(require("remark-math")),w=u(require("remark-frontmatter")),v=require("unist-util-visit");function k(t,e){return{type:t,children:e,data:{hName:t}}}var q={mark:(t,e,r)=>"=="+r.containerPhrasing(t,{before:"==",after:"=="})+"==",sub:(t,e,r)=>"~"+r.containerPhrasing(t,{before:"~",after:"~"})+"~",sup:(t,e,r)=>"^"+r.containerPhrasing(t,{before:"^",after:"^"})+"^"},j={plugin:()=>t=>{!function(t){(0,v.visit)(t,"text",(t,e,r)=>{if(!r||void 0===e)return;const i=t.value;let n=0;const s=[];let o=!1;const a=/(==[^=]+==|~[^~]+~|\^[^^]+\^)/g;let u;for(;null!==(u=a.exec(i));){o=!0;const t=u[0],e=u.index;e>n&&s.push({type:"text",value:i.slice(n,e)});let r="mark",l="";t.startsWith("==")?(r="mark",l=t.slice(2,-2)):t.startsWith("~")?(r="sub",l=t.slice(1,-1)):t.startsWith("^")&&(r="sup",l=t.slice(1,-1)),s.push(k(r,[{type:"text",value:l}])),n=a.lastIndex}return o?(n<i.length&&s.push({type:"text",value:i.slice(n)}),r.children.splice(e,1,...s),e+s.length):void 0})}(t)},stage:100},M=require("unist-util-visit"),P={error:"danger",warn:"warning",success:"tip",important:"important",caution:"caution",note:"note"},x={plugin:()=>async t=>{(0,M.visit)(t,["containerDirective","leafDirective","textDirective"],t=>{const e=t,r=e.name.toLowerCase();if(e.name=P[r]||r,e.children&&e.children.length>0){const t=e.children[0];if(t.data?.directiveLabel||"directiveLabel"===t.type){const r=t;let i="";(0,M.visit)(r,"text",t=>{i+=t.value}),i&&!e.attributes?.title&&(e.attributes=e.attributes||{},e.attributes.title=i.trim()),e.children.shift()}}e.attributes?.title&&(e.attributes.title=String(e.attributes.title).trim()),e.data=e.data||{},e.data.hName=e.data.hName||("containerDirective"===e.type?"div":"span"),e.data.hProperties={...e.data.hProperties||{},...e.attributes,className:[e.name,e.data.hProperties?.className].filter(Boolean).join(" ")}})},stage:100,order:10},S=require("unist-util-visit"),F={plugin:()=>async t=>{(0,S.visit)(t,"tableCell",t=>{if(t.data){const{rowspan:e,colspan:r}=t.data;t.data.hProperties=t.data.hProperties||{},void 0!==e&&(t.data.hProperties.rowSpan=e,delete t.data.rowspan),void 0!==r&&(t.data.hProperties.colSpan=r,delete t.data.colspan)}})},stage:100,order:20},T=require("unist-util-visit"),O=require("shell-quote");var A={plugin:()=>async t=>{(0,T.visit)(t,"code",t=>{if(t.meta){const e=function(t){const e={},r=(0,O.parse)(t);for(const t of r)if("string"==typeof t){const r=t.split("=",2);2===r.length?e[r[0]]=r[1]:e[t]="true"}return e}(t.meta),r=t.data=t.data||{};e.title&&(r.title=e.title),e.filename&&(r.filename=e.filename),r.kv={...r.kv||{},...e}}})},stage:100,order:30},D=require("unist-util-visit"),N={plugin:()=>async t=>{(0,D.visit)(t,"image",t=>{const e=t.data=t.data||{},r=e.hProperties=e.hProperties||{},i=/[#?&](?:width=([0-9]+))?(?:&?height=([0-9]+))?(?:=([0-9]+)x([0-9]+))?$/,n=t.url.match(i);if(n){const e=n[1]||n[3],s=n[2]||n[4];e&&!r.width&&(r.width=parseInt(e,10)),s&&!r.height&&(r.height=parseInt(s,10)),t.url=t.url.replace(i,"")}e.width&&!r.width&&(r.width=e.width),e.height&&!r.height&&(r.height=e.height)})},stage:100,order:40},E=[{plugin:g.default,stage:0},{plugin:d.default,options:[{singleTilde:!1}],stage:0},{plugin:y.default,stage:0},{plugin:b.default,stage:0},{plugin:w.default,options:[["yaml","toml"]],stage:0},x,F,A,N,j],z=[{plugin:m.default,options:[{handlers:q}],stage:400},{plugin:d.default,options:[{singleTilde:!1}],stage:400},{plugin:y.default,stage:400},{plugin:b.default,stage:400},{plugin:w.default,options:[["yaml","toml"]],stage:400}];z.forEach(t=>{t.plugin===m.default?t.order=100:t.order=10});var R={id:"markdown",title:"Markdown (GFM + Directives)",extensions:["md","markdown","mdown","mkdn"],mediaTypes:["text/markdown"],input:E,output:z},$=u(require("rehype-parse")),B=u(require("rehype-remark")),C=u(require("remark-rehype")),H=u(require("rehype-sanitize")),I=u(require("rehype-stringify")),L={id:"html",title:"HTML",extensions:["html","htm"],mediaTypes:["text/html"],input:[{name:"rehype-parse",plugin:$.default,stage:0},{name:"rehype-remark",plugin:B.default,options:[{handlers:{mark:(t,e)=>{const r={type:"mark",children:t.all(e)};return t.patch(e,r),r},sub:(t,e)=>{const r={type:"sub",children:t.all(e)};return t.patch(e,r),r},sup:(t,e)=>{const r={type:"sup",children:t.all(e)};return t.patch(e,r),r}}}],stage:0}],output:[{plugin:C.default,stage:300,order:10},{plugin:H.default,options:[{...H.defaultSchema,tagNames:[...H.defaultSchema.tagNames||[],"mark","sub","sup"],attributes:{...H.defaultSchema.attributes,"*":[...H.defaultSchema.attributes?.["*"]||[],"className","id","style"],td:[...H.defaultSchema.attributes?.td||[],"rowSpan","colSpan","rowspan","colspan"],th:[...H.defaultSchema.attributes?.th||[],"rowSpan","colSpan","rowspan","colspan"],img:[...H.defaultSchema.attributes?.img||[],"width","height"]}}],stage:300,order:20},{plugin:I.default,stage:400}]};function V(){this.Compiler=t=>t}function _(){this.Parser=t=>JSON.parse(t)}var G={id:"ast",title:"MDAST",input:[{plugin:_,stage:0},x,F,A,N,j],output:[{plugin:V,options:[],stage:400}]};function J(t){return"object"==typeof t&&null!==t&&"string"==typeof t.type}var K=class t{constructor(t){this.queue=[],this._data={},this.input=t}static register(t){this.registry.set(t.id,t)}static getFormat(t){return this.registry.get(t)}data(t,e){return"string"==typeof t?this._data[t]=e:Object.assign(this._data,t),this}getFormat(t){return this.constructor.getFormat(t)}resolveFormat(t){if("string"==typeof t){const e=this.getFormat(t);if(!e)throw new Error(`[MdastPlus] Format '${t}' is not registered.`);return e}return t}toRuntimeEntry(t,e,r){let i=e;void 0!==t.stage&&(i="string"==typeof t.stage?c[t.stage]??e:t.stage);let n=t.options||[];const s=t.name||t.plugin.name;if(r&&s&&s in r){const e=r[s];"object"!=typeof e||null===e||Array.isArray(e)?n=Array.isArray(e)?e:[e]:("main"in e&&(t.main=!!e.main),"before"in e&&(t.before=e.before),"after"in e&&(t.after=e.after),n=[e])}return{name:s,plugin:t.plugin,options:n,stage:i,order:t.order||0,main:t.main,before:t.before,after:t.after}}ensureInputPlugins(e,r,i=400){const n=e.some(t=>0===(t.stage??p)),s=J(this.input);if(!n){let n=[];if(s){const e=t.getFormat("ast");e&&e.input&&(n=e.input)}else{const e=t.getFormat("markdown");e&&e.input&&(n=e.input)}for(const t of n){const n=this.toRuntimeEntry(t,0,r);(n.stage??p)<=i&&e.push(n)}}}from(t,e){const r=this.resolveFormat(t);if(!r.input||0===r.input.length)throw new Error(`[MdastPlus] Format '${r.id}' does not support input.`);for(const t of r.input)this.queue.push(this.toRuntimeEntry(t,0,e));return this}async to(t,e){const r=this.resolveFormat(t);if(!r.output)throw new Error(`[MdastPlus] Format '${r.id}' does not support output.`);const i=[...this.queue];this.ensureInputPlugins(i,e);for(const t of r.output)i.push(this.toRuntimeEntry(t,300,e));const n=this.assembleProcessor(i);if(J(this.input)){const t=await n.run(this.input),e=n.stringify(t),r=new h.VFile;return"string"==typeof e||Buffer.isBuffer(e)?r.value=e:r.result=e,r}return n.process(this.input)}use(t,...e){return this.useAt("compile",t,...e)}useAt(t,e,...r){if(Array.isArray(t)){for(const i of t)this.useAt(i,e,...r);return this}if(Array.isArray(e)){for(const i of e)this.useAt(t,i,...r);return this}if("object"==typeof t&&null!==t&&"plugin"in t){const i=t,n=void 0!==i.stage?"string"==typeof i.stage?c[i.stage]:i.stage:p,s=void 0!==e?[e,...r]:i.options;this.queue.push(this.toRuntimeEntry({...i,options:s},n))}else{const i="string"==typeof t?c[t]??p:t;if("object"==typeof e&&null!==e&&"plugin"in e){const t=e,n=r.length>0?r:t.options;this.queue.push(this.toRuntimeEntry({...t,options:n},i))}else e&&this.queue.push({plugin:e,options:r,stage:i,order:0})}return this}priority(t){const e=this.queue[this.queue.length-1];return e&&(e.order=t),this}configure(t,...e){for(let r=this.queue.length-1;r>=0;r--){const i=this.queue[r];if((i.name||i.plugin.name)===t){i.options=e;break}}return this}assembleProcessor(t){const e={};for(const r of t){const t=r.stage??p;e[t]||(e[t]=[]),e[t].push(r)}const r=[],i=Object.keys(e).map(Number).sort((t,e)=>t-e);for(const t of i){const i=e[t].sort((t,e)=>(t.order||0)-(e.order||0)),n=i.findIndex(t=>t.main);if(-1!==n){const t=i[n];!1===t.options?.[0]?console.warn(`Main Plugin "${t.name}" is disabled. Skipping.`):(i.splice(n,1),i[0]=t)}let s=!0,o=0;for(;s&&o<i.length;){s=!1,o++;for(let t=0;t<i.length;t++){const e=i[t];if(e.after){const r=i.findIndex(t=>t.name===e.after);if(-1!==r&&r>t){i.splice(t,1),i.splice(r,0,e),s=!0;break}}if(e.before){const r=i.findIndex(t=>t.name===e.before);if(-1!==r&&r<t){i.splice(t,1),i.splice(r,0,e),s=!0;break}}}}r.push(...i)}const n=(0,f.unified)();Object.keys(this._data).length>0&&n.data(this._data);for(const t of r)n.use(t.plugin,...t.options||[]);return n}};K.registry=new Map;var Q=K,U=class extends Q{async toMarkdown(){const t=await this.to("markdown");return String(t)}toMarkdownVFile(){return this.to("markdown")}async toHtml(){const t=await this.to("html");return String(t)}toHtmlVFile(){return this.to("html")}async toAst(t){if(t?.stage){const e="string"==typeof t.stage?c[t.stage]:t.stage,r=this.queue.filter(t=>(t.stage??p)<=e);r.push({plugin:V,options:[],stage:400,order:0}),this.ensureInputPlugins(r,t.overrides,e);const i=this.assembleProcessor(r);if(J(this.input)){return await i.run(this.input)}return(await i.process(this.input)).result}return(await this.to("ast",t?.overrides)).result}toHTML(){return this.toHtml()}toAST(t){return this.toAst(t)}};function W(t){return new U(t)}U.register(R),U.register(L),U.register(G);var X=require("hast-util-from-html"),Y=function(t){const{readability:e,jsdom:r,url:i}=t||{};this.parser=function(t,n){if(!1===e)return(0,X.fromHtml)(t,{fragment:!0});let s,o;try{s=require("jsdom").JSDOM;o=require("@mozilla/readability").Readability}catch(t){throw new Error("[html-readability] Dependency missing. Please install 'jsdom' and '@mozilla/readability'.")}const a=new o(new s(t,{url:i,pretendToBeVisual:!0,...r}).window.document,{maxElemsToParse:1e5,nbTopCandidates:5,charThreshold:500,keepClasses:!0,...e}).parse();if(!a||!a.content)return(0,X.fromHtml)(t,{fragment:!0});const u=(0,X.fromHtml)(a.content,{fragment:!0}),l={title:a.title,byline:a.byline,excerpt:a.excerpt,siteName:a.siteName,lang:a.lang};return n&&(n.data=n.data||{},n.data.readability=l),u&&(u.data=u.data||{},u.data.readability=l),u}},Z={name:"readability",plugin:Y,stage:0,main:!0},tt={name:"restore-readability-meta",plugin:()=>(t,e)=>{e.data?.readability&&(t.data=t.data||{},t.data.readability=e.data.readability)},stage:0,after:"rehype-remark"},et=[Z,tt];
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var t=(t=>"undefined"!=typeof require?require:"undefined"!=typeof Proxy?new Proxy(t,{get:(t,e)=>("undefined"!=typeof require?require:t)[e]}):t)(function(t){if("undefined"!=typeof require)return require.apply(this,arguments);throw Error('Dynamic require of "'+t+'" is not supported')}),e=(t=>(t[t.parse=0]="parse",t[t.normalize=100]="normalize",t[t.compile=200]="compile",t[t.finalize=300]="finalize",t[t.stringify=400]="stringify",t))(e||{}),r=200;import{unified as i}from"unified";import{VFile as n}from"vfile";import o from"remark-parse";import s from"remark-stringify";import a from"remark-gfm";import u from"remark-directive";import p from"remark-math";import l from"remark-frontmatter";import{visit as f}from"unist-util-visit";function m(t,e){return{type:t,children:e,data:{hName:t}}}var c={plugin:()=>t=>{!function(t){f(t,"text",(t,e,r)=>{if(!r||void 0===e)return;const i=t.value;let n=0;const o=[];let s=!1;const a=/(==[^=]+==|~[^~]+~|\^[^^]+\^)/g;let u;for(;null!==(u=a.exec(i));){s=!0;const t=u[0],e=u.index;e>n&&o.push({type:"text",value:i.slice(n,e)});let r="mark",p="";t.startsWith("==")?(r="mark",p=t.slice(2,-2)):t.startsWith("~")?(r="sub",p=t.slice(1,-1)):t.startsWith("^")&&(r="sup",p=t.slice(1,-1)),o.push(m(r,[{type:"text",value:p}])),n=a.lastIndex}return s?(n<i.length&&o.push({type:"text",value:i.slice(n)}),r.children.splice(e,1,...o),e+o.length):void 0})}(t)},stage:100};import{visit as h}from"unist-util-visit";var g={error:"danger",warn:"warning",success:"tip",important:"important",caution:"caution",note:"note"},d={plugin:()=>async t=>{h(t,["containerDirective","leafDirective","textDirective"],t=>{const e=t,r=e.name.toLowerCase();if(e.name=g[r]||r,e.children&&e.children.length>0){const t=e.children[0];if(t.data?.directiveLabel||"directiveLabel"===t.type){let r="";h(t,"text",t=>{r+=t.value}),r&&!e.attributes?.title&&(e.attributes=e.attributes||{},e.attributes.title=r.trim()),e.children.shift()}}e.attributes?.title&&(e.attributes.title=String(e.attributes.title).trim()),e.data=e.data||{},e.data.hName=e.data.hName||("containerDirective"===e.type?"div":"span"),e.data.hProperties={...e.data.hProperties||{},...e.attributes,className:[e.name,e.data.hProperties?.className].filter(Boolean).join(" ")}})},stage:100,order:10};import{visit as y}from"unist-util-visit";var v={plugin:()=>async t=>{y(t,"tableCell",t=>{if(t.data){const{rowspan:e,colspan:r}=t.data;t.data.hProperties=t.data.hProperties||{},void 0!==e&&(t.data.hProperties.rowSpan=e,delete t.data.rowspan),void 0!==r&&(t.data.hProperties.colSpan=r,delete t.data.colspan)}})},stage:100,order:20};import{visit as w}from"unist-util-visit";import{parse as b}from"shell-quote";var k={plugin:()=>async t=>{w(t,"code",t=>{if(t.meta){const e=function(t){const e={},r=b(t);for(const t of r)if("string"==typeof t){const r=t.split("=",2);2===r.length?e[r[0]]=r[1]:e[t]="true"}return e}(t.meta),r=t.data=t.data||{};e.title&&(r.title=e.title),e.filename&&(r.filename=e.filename),r.kv={...r.kv||{},...e}}})},stage:100,order:30};import{visit as x}from"unist-util-visit";var M={plugin:()=>async t=>{x(t,"image",t=>{const e=t.data=t.data||{},r=e.hProperties=e.hProperties||{},i=/[#?&](?:width=([0-9]+))?(?:&?height=([0-9]+))?(?:=([0-9]+)x([0-9]+))?$/,n=t.url.match(i);if(n){const e=n[1]||n[3],o=n[2]||n[4];e&&!r.width&&(r.width=parseInt(e,10)),o&&!r.height&&(r.height=parseInt(o,10)),t.url=t.url.replace(i,"")}e.width&&!r.width&&(r.width=e.width),e.height&&!r.height&&(r.height=e.height)})},stage:100,order:40},j=[{plugin:o,stage:0},{plugin:a,options:[{singleTilde:!1}],stage:0},{plugin:u,stage:0},{plugin:p,stage:0},{plugin:l,options:[["yaml","toml"]],stage:0},d,v,k,M,c],S=[{plugin:s,options:[{handlers:{mark:(t,e,r)=>"=="+r.containerPhrasing(t,{before:"==",after:"=="})+"==",sub:(t,e,r)=>"~"+r.containerPhrasing(t,{before:"~",after:"~"})+"~",sup:(t,e,r)=>"^"+r.containerPhrasing(t,{before:"^",after:"^"})+"^"}}],stage:400},{plugin:a,options:[{singleTilde:!1}],stage:400},{plugin:u,stage:400},{plugin:p,stage:400},{plugin:l,options:[["yaml","toml"]],stage:400}];S.forEach(t=>{t.plugin===s?t.order=100:t.order=10});var F={id:"markdown",title:"Markdown (GFM + Directives)",extensions:["md","markdown","mdown","mkdn"],mediaTypes:["text/markdown"],input:j,output:S};import q from"rehype-parse";import A from"rehype-remark";import D from"remark-rehype";import P,{defaultSchema as T}from"rehype-sanitize";import N from"rehype-stringify";var E={id:"html",title:"HTML",extensions:["html","htm"],mediaTypes:["text/html"],input:[{name:"rehype-parse",plugin:q,stage:0},{name:"rehype-remark",plugin:A,options:[{handlers:{mark:(t,e)=>{const r={type:"mark",children:t.all(e)};return t.patch(e,r),r},sub:(t,e)=>{const r={type:"sub",children:t.all(e)};return t.patch(e,r),r},sup:(t,e)=>{const r={type:"sup",children:t.all(e)};return t.patch(e,r),r}}}],stage:0}],output:[{plugin:D,stage:300,order:10},{plugin:P,options:[{...T,tagNames:[...T.tagNames||[],"mark","sub","sup"],attributes:{...T.attributes,"*":[...T.attributes?.["*"]||[],"className","id","style"],td:[...T.attributes?.td||[],"rowSpan","colSpan","rowspan","colspan"],th:[...T.attributes?.th||[],"rowSpan","colSpan","rowspan","colspan"],img:[...T.attributes?.img||[],"width","height"]}}],stage:300,order:20},{plugin:N,stage:400}]};function z(){this.Compiler=t=>t}function H(){this.Parser=t=>JSON.parse(t)}var O={id:"ast",title:"MDAST",input:[{plugin:H,stage:0},d,v,k,M,c],output:[{plugin:z,options:[],stage:400}]};function $(t){return"object"==typeof t&&null!==t&&"string"==typeof t.type}var I=class t{constructor(t){this.queue=[],this._data={},this.input=t}static register(t){this.registry.set(t.id,t)}static getFormat(t){return this.registry.get(t)}data(t,e){return"string"==typeof t?this._data[t]=e:Object.assign(this._data,t),this}getFormat(t){return this.constructor.getFormat(t)}resolveFormat(t){if("string"==typeof t){const e=this.getFormat(t);if(!e)throw new Error(`[MdastPlus] Format '${t}' is not registered.`);return e}return t}toRuntimeEntry(t,r,i){let n=r;void 0!==t.stage&&(n="string"==typeof t.stage?e[t.stage]??r:t.stage);let o=t.options||[];const s=t.name||t.plugin.name;if(i&&s&&s in i){const e=i[s];"object"!=typeof e||null===e||Array.isArray(e)?o=Array.isArray(e)?e:[e]:("main"in e&&(t.main=!!e.main),"before"in e&&(t.before=e.before),"after"in e&&(t.after=e.after),o=[e])}return{name:s,plugin:t.plugin,options:o,stage:n,order:t.order||0,main:t.main,before:t.before,after:t.after}}ensureInputPlugins(e,i,n=400){const o=e.some(t=>0===(t.stage??r)),s=$(this.input);if(!o){let o=[];if(s){const e=t.getFormat("ast");e&&e.input&&(o=e.input)}else{const e=t.getFormat("markdown");e&&e.input&&(o=e.input)}for(const t of o){const o=this.toRuntimeEntry(t,0,i);(o.stage??r)<=n&&e.push(o)}}}from(t,e){const r=this.resolveFormat(t);if(!r.input||0===r.input.length)throw new Error(`[MdastPlus] Format '${r.id}' does not support input.`);for(const t of r.input)this.queue.push(this.toRuntimeEntry(t,0,e));return this}async to(t,e){const r=this.resolveFormat(t);if(!r.output)throw new Error(`[MdastPlus] Format '${r.id}' does not support output.`);const i=[...this.queue];this.ensureInputPlugins(i,e);for(const t of r.output)i.push(this.toRuntimeEntry(t,300,e));const o=this.assembleProcessor(i);if($(this.input)){const t=await o.run(this.input),e=o.stringify(t),r=new n;return"string"==typeof e||Buffer.isBuffer(e)?r.value=e:r.result=e,r}return o.process(this.input)}use(t,...e){return this.useAt("compile",t,...e)}useAt(t,r,...i){if(Array.isArray(t)){for(const e of t)this.useAt(e,r,...i);return this}if(Array.isArray(r)){for(const e of r)this.useAt(t,e,...i);return this}if("object"==typeof t&&null!==t&&"plugin"in t){const n=t,o=void 0!==n.stage?"string"==typeof n.stage?n.stage:e[n.stage]:"compile",s=void 0!==r?[r,...i]:n.options;this.queue.push(this.toRuntimeEntry({...n,options:s},e[o]))}else{const n=t;if("object"==typeof r&&null!==r&&"plugin"in r){const t=r,o=i.length>0?i:t.options;this.queue.push(this.toRuntimeEntry({...t,options:o},e[n]))}else r&&this.queue.push({plugin:r,options:i,stage:e[n],order:0})}return this}priority(t){const e=this.queue[this.queue.length-1];return e&&(e.order=t),this}assembleProcessor(t){const e={};for(const i of t){const t=i.stage??r;e[t]||(e[t]=[]),e[t].push(i)}const n=[],o=Object.keys(e).map(Number).sort((t,e)=>t-e);for(const t of o){const r=e[t].sort((t,e)=>(t.order||0)-(e.order||0)),i=r.findIndex(t=>t.main);if(-1!==i){const t=r[i];r.splice(i,1),r[0]=t}let o=!0,s=0;for(;o&&s<r.length;){o=!1,s++;for(let t=0;t<r.length;t++){const e=r[t];if(e.after){const i=r.findIndex(t=>t.name===e.after);if(-1!==i&&i>t){r.splice(t,1),r.splice(i,0,e),o=!0;break}}if(e.before){const i=r.findIndex(t=>t.name===e.before);if(-1!==i&&i<t){r.splice(t,1),r.splice(i,0,e),o=!0;break}}}}n.push(...r)}const s=i();Object.keys(this._data).length>0&&s.data(this._data);for(const t of n)s.use(t.plugin,...t.options||[]);return s}};I.registry=new Map;var L=I,B=class extends L{async toMarkdown(){const t=await this.to("markdown");return String(t)}toMarkdownVFile(){return this.to("markdown")}async toHtml(){const t=await this.to("html");return String(t)}toHtmlVFile(){return this.to("html")}async toAst(t){if(t?.stage){const i=e[t.stage],n=this.queue.filter(t=>(t.stage??r)<=i);n.push({plugin:z,options:[],stage:400,order:0}),this.ensureInputPlugins(n,t.overrides,i);const o=this.assembleProcessor(n);if($(this.input)){return await o.run(this.input)}return(await o.process(this.input)).result}return(await this.to("ast",t?.overrides)).result}toHTML(){return this.toHtml()}toAST(t){return this.toAst(t)}};function V(t){return new B(t)}B.register(F),B.register(E),B.register(O);import{fromHtml as C}from"hast-util-from-html";var G=function(e){let r={};"boolean"==typeof e?r={enable:e}:e&&(r=e);const{enable:i,readability:n,jsdom:o}=r;!1!==i&&(this.parser=function(e,r){let i,s;try{i=t("jsdom").JSDOM;s=t("@mozilla/readability").Readability}catch(t){throw new Error("[html-readability] Dependency missing. Please install 'jsdom' and '@mozilla/readability'.")}const a=new s(new i(e,o).window.document,n).parse();if(!a||!a.content)return C(e,{fragment:!0});const u=C(a.content,{fragment:!0}),p={title:a.title,byline:a.byline,excerpt:a.excerpt,siteName:a.siteName,lang:a.lang};return r&&(r.data=r.data||{},r.data.readability=p),u&&(u.data=u.data||{},u.data.readability=p),u})},J={name:"readability",plugin:G,stage:0},R={name:"restore-readability-meta",plugin:()=>(t,e)=>{e.data?.readability&&(t.data=t.data||{},t.data.readability=e.data.readability)},stage:0,after:"rehype-remark"},K=[J,R];export{r as DefaultPipelineStage,L as MdastBasePipeline,B as MdastPipeline,e as PipelineStage,z as astCompiler,O as astFormat,E as htmlFormat,G as htmlReadability,J as htmlReadabilityPlugin,K as htmlReadabilityPlugins,H as jsonParser,F as markdownFormat,V as mdast,R as restoreReadabilityMetaPlugin};
|
|
1
|
+
var t=(t=>"undefined"!=typeof require?require:"undefined"!=typeof Proxy?new Proxy(t,{get:(t,e)=>("undefined"!=typeof require?require:t)[e]}):t)(function(t){if("undefined"!=typeof require)return require.apply(this,arguments);throw Error('Dynamic require of "'+t+'" is not supported')}),e=(t=>(t[t.parse=0]="parse",t[t.normalize=100]="normalize",t[t.compile=200]="compile",t[t.finalize=300]="finalize",t[t.stringify=400]="stringify",t))(e||{}),r=200;import{unified as i}from"unified";import{VFile as n}from"vfile";import o from"remark-parse";import s from"remark-stringify";import a from"remark-gfm";import u from"remark-directive";import p from"remark-math";import l from"remark-frontmatter";import{visit as f}from"unist-util-visit";function m(t,e){return{type:t,children:e,data:{hName:t}}}var c={plugin:()=>t=>{!function(t){f(t,"text",(t,e,r)=>{if(!r||void 0===e)return;const i=t.value;let n=0;const o=[];let s=!1;const a=/(==[^=]+==|~[^~]+~|\^[^^]+\^)/g;let u;for(;null!==(u=a.exec(i));){s=!0;const t=u[0],e=u.index;e>n&&o.push({type:"text",value:i.slice(n,e)});let r="mark",p="";t.startsWith("==")?(r="mark",p=t.slice(2,-2)):t.startsWith("~")?(r="sub",p=t.slice(1,-1)):t.startsWith("^")&&(r="sup",p=t.slice(1,-1)),o.push(m(r,[{type:"text",value:p}])),n=a.lastIndex}return s?(n<i.length&&o.push({type:"text",value:i.slice(n)}),r.children.splice(e,1,...o),e+o.length):void 0})}(t)},stage:100};import{visit as h}from"unist-util-visit";var g={error:"danger",warn:"warning",success:"tip",important:"important",caution:"caution",note:"note"},d={plugin:()=>async t=>{h(t,["containerDirective","leafDirective","textDirective"],t=>{const e=t,r=e.name.toLowerCase();if(e.name=g[r]||r,e.children&&e.children.length>0){const t=e.children[0];if(t.data?.directiveLabel||"directiveLabel"===t.type){let r="";h(t,"text",t=>{r+=t.value}),r&&!e.attributes?.title&&(e.attributes=e.attributes||{},e.attributes.title=r.trim()),e.children.shift()}}e.attributes?.title&&(e.attributes.title=String(e.attributes.title).trim()),e.data=e.data||{},e.data.hName=e.data.hName||("containerDirective"===e.type?"div":"span"),e.data.hProperties={...e.data.hProperties||{},...e.attributes,className:[e.name,e.data.hProperties?.className].filter(Boolean).join(" ")}})},stage:100,order:10};import{visit as y}from"unist-util-visit";var v={plugin:()=>async t=>{y(t,"tableCell",t=>{if(t.data){const{rowspan:e,colspan:r}=t.data;t.data.hProperties=t.data.hProperties||{},void 0!==e&&(t.data.hProperties.rowSpan=e,delete t.data.rowspan),void 0!==r&&(t.data.hProperties.colSpan=r,delete t.data.colspan)}})},stage:100,order:20};import{visit as w}from"unist-util-visit";import{parse as b}from"shell-quote";var k={plugin:()=>async t=>{w(t,"code",t=>{if(t.meta){const e=function(t){const e={},r=b(t);for(const t of r)if("string"==typeof t){const r=t.split("=",2);2===r.length?e[r[0]]=r[1]:e[t]="true"}return e}(t.meta),r=t.data=t.data||{};e.title&&(r.title=e.title),e.filename&&(r.filename=e.filename),r.kv={...r.kv||{},...e}}})},stage:100,order:30};import{visit as x}from"unist-util-visit";var M={plugin:()=>async t=>{x(t,"image",t=>{const e=t.data=t.data||{},r=e.hProperties=e.hProperties||{},i=/[#?&](?:width=([0-9]+))?(?:&?height=([0-9]+))?(?:=([0-9]+)x([0-9]+))?$/,n=t.url.match(i);if(n){const e=n[1]||n[3],o=n[2]||n[4];e&&!r.width&&(r.width=parseInt(e,10)),o&&!r.height&&(r.height=parseInt(o,10)),t.url=t.url.replace(i,"")}e.width&&!r.width&&(r.width=e.width),e.height&&!r.height&&(r.height=e.height)})},stage:100,order:40},T=[{plugin:o,stage:0},{plugin:a,options:[{singleTilde:!1}],stage:0},{plugin:u,stage:0},{plugin:p,stage:0},{plugin:l,options:[["yaml","toml"]],stage:0},d,v,k,M,c],S=[{plugin:s,options:[{handlers:{mark:(t,e,r)=>"=="+r.containerPhrasing(t,{before:"==",after:"=="})+"==",sub:(t,e,r)=>"~"+r.containerPhrasing(t,{before:"~",after:"~"})+"~",sup:(t,e,r)=>"^"+r.containerPhrasing(t,{before:"^",after:"^"})+"^"}}],stage:400},{plugin:a,options:[{singleTilde:!1}],stage:400},{plugin:u,stage:400},{plugin:p,stage:400},{plugin:l,options:[["yaml","toml"]],stage:400}];S.forEach(t=>{t.plugin===s?t.order=100:t.order=10});var j={id:"markdown",title:"Markdown (GFM + Directives)",extensions:["md","markdown","mdown","mkdn"],mediaTypes:["text/markdown"],input:T,output:S};import P from"rehype-parse";import F from"rehype-remark";import q from"remark-rehype";import A,{defaultSchema as D}from"rehype-sanitize";import E from"rehype-stringify";var N={id:"html",title:"HTML",extensions:["html","htm"],mediaTypes:["text/html"],input:[{name:"rehype-parse",plugin:P,stage:0},{name:"rehype-remark",plugin:F,options:[{handlers:{mark:(t,e)=>{const r={type:"mark",children:t.all(e)};return t.patch(e,r),r},sub:(t,e)=>{const r={type:"sub",children:t.all(e)};return t.patch(e,r),r},sup:(t,e)=>{const r={type:"sup",children:t.all(e)};return t.patch(e,r),r}}}],stage:0}],output:[{plugin:q,stage:300,order:10},{plugin:A,options:[{...D,tagNames:[...D.tagNames||[],"mark","sub","sup"],attributes:{...D.attributes,"*":[...D.attributes?.["*"]||[],"className","id","style"],td:[...D.attributes?.td||[],"rowSpan","colSpan","rowspan","colspan"],th:[...D.attributes?.th||[],"rowSpan","colSpan","rowspan","colspan"],img:[...D.attributes?.img||[],"width","height"]}}],stage:300,order:20},{plugin:E,stage:400}]};function z(){this.Compiler=t=>t}function $(){this.Parser=t=>JSON.parse(t)}var H={id:"ast",title:"MDAST",input:[{plugin:$,stage:0},d,v,k,M,c],output:[{plugin:z,options:[],stage:400}]};function O(t){return"object"==typeof t&&null!==t&&"string"==typeof t.type}var B=class t{constructor(t){this.queue=[],this._data={},this.input=t}static register(t){this.registry.set(t.id,t)}static getFormat(t){return this.registry.get(t)}data(t,e){return"string"==typeof t?this._data[t]=e:Object.assign(this._data,t),this}getFormat(t){return this.constructor.getFormat(t)}resolveFormat(t){if("string"==typeof t){const e=this.getFormat(t);if(!e)throw new Error(`[MdastPlus] Format '${t}' is not registered.`);return e}return t}toRuntimeEntry(t,r,i){let n=r;void 0!==t.stage&&(n="string"==typeof t.stage?e[t.stage]??r:t.stage);let o=t.options||[];const s=t.name||t.plugin.name;if(i&&s&&s in i){const e=i[s];"object"!=typeof e||null===e||Array.isArray(e)?o=Array.isArray(e)?e:[e]:("main"in e&&(t.main=!!e.main),"before"in e&&(t.before=e.before),"after"in e&&(t.after=e.after),o=[e])}return{name:s,plugin:t.plugin,options:o,stage:n,order:t.order||0,main:t.main,before:t.before,after:t.after}}ensureInputPlugins(e,i,n=400){const o=e.some(t=>0===(t.stage??r)),s=O(this.input);if(!o){let o=[];if(s){const e=t.getFormat("ast");e&&e.input&&(o=e.input)}else{const e=t.getFormat("markdown");e&&e.input&&(o=e.input)}for(const t of o){const o=this.toRuntimeEntry(t,0,i);(o.stage??r)<=n&&e.push(o)}}}from(t,e){const r=this.resolveFormat(t);if(!r.input||0===r.input.length)throw new Error(`[MdastPlus] Format '${r.id}' does not support input.`);for(const t of r.input)this.queue.push(this.toRuntimeEntry(t,0,e));return this}async to(t,e){const r=this.resolveFormat(t);if(!r.output)throw new Error(`[MdastPlus] Format '${r.id}' does not support output.`);const i=[...this.queue];this.ensureInputPlugins(i,e);for(const t of r.output)i.push(this.toRuntimeEntry(t,300,e));const o=this.assembleProcessor(i);if(O(this.input)){const t=await o.run(this.input),e=o.stringify(t),r=new n;return"string"==typeof e||Buffer.isBuffer(e)?r.value=e:r.result=e,r}return o.process(this.input)}use(t,...e){return this.useAt("compile",t,...e)}useAt(t,i,...n){if(Array.isArray(t)){for(const e of t)this.useAt(e,i,...n);return this}if(Array.isArray(i)){for(const e of i)this.useAt(t,e,...n);return this}if("object"==typeof t&&null!==t&&"plugin"in t){const o=t,s=void 0!==o.stage?"string"==typeof o.stage?e[o.stage]:o.stage:r,a=void 0!==i?[i,...n]:o.options;this.queue.push(this.toRuntimeEntry({...o,options:a},s))}else{const o="string"==typeof t?e[t]??r:t;if("object"==typeof i&&null!==i&&"plugin"in i){const t=i,e=n.length>0?n:t.options;this.queue.push(this.toRuntimeEntry({...t,options:e},o))}else i&&this.queue.push({plugin:i,options:n,stage:o,order:0})}return this}priority(t){const e=this.queue[this.queue.length-1];return e&&(e.order=t),this}configure(t,...e){for(let r=this.queue.length-1;r>=0;r--){const i=this.queue[r];if((i.name||i.plugin.name)===t){i.options=e;break}}return this}assembleProcessor(t){const e={};for(const i of t){const t=i.stage??r;e[t]||(e[t]=[]),e[t].push(i)}const n=[],o=Object.keys(e).map(Number).sort((t,e)=>t-e);for(const t of o){const r=e[t].sort((t,e)=>(t.order||0)-(e.order||0)),i=r.findIndex(t=>t.main);if(-1!==i){const t=r[i];!1===t.options?.[0]?console.warn(`Main Plugin "${t.name}" is disabled. Skipping.`):(r.splice(i,1),r[0]=t)}let o=!0,s=0;for(;o&&s<r.length;){o=!1,s++;for(let t=0;t<r.length;t++){const e=r[t];if(e.after){const i=r.findIndex(t=>t.name===e.after);if(-1!==i&&i>t){r.splice(t,1),r.splice(i,0,e),o=!0;break}}if(e.before){const i=r.findIndex(t=>t.name===e.before);if(-1!==i&&i<t){r.splice(t,1),r.splice(i,0,e),o=!0;break}}}}n.push(...r)}const s=i();Object.keys(this._data).length>0&&s.data(this._data);for(const t of n)s.use(t.plugin,...t.options||[]);return s}};B.registry=new Map;var C=B,I=class extends C{async toMarkdown(){const t=await this.to("markdown");return String(t)}toMarkdownVFile(){return this.to("markdown")}async toHtml(){const t=await this.to("html");return String(t)}toHtmlVFile(){return this.to("html")}async toAst(t){if(t?.stage){const i="string"==typeof t.stage?e[t.stage]:t.stage,n=this.queue.filter(t=>(t.stage??r)<=i);n.push({plugin:z,options:[],stage:400,order:0}),this.ensureInputPlugins(n,t.overrides,i);const o=this.assembleProcessor(n);if(O(this.input)){return await o.run(this.input)}return(await o.process(this.input)).result}return(await this.to("ast",t?.overrides)).result}toHTML(){return this.toHtml()}toAST(t){return this.toAst(t)}};function L(t){return new I(t)}I.register(j),I.register(N),I.register(H);import{fromHtml as V}from"hast-util-from-html";var G=function(e){const{readability:r,jsdom:i,url:n}=e||{};this.parser=function(e,o){if(!1===r)return V(e,{fragment:!0});let s,a;try{s=t("jsdom").JSDOM;a=t("@mozilla/readability").Readability}catch(t){throw new Error("[html-readability] Dependency missing. Please install 'jsdom' and '@mozilla/readability'.")}const u=new a(new s(e,{url:n,pretendToBeVisual:!0,...i}).window.document,{maxElemsToParse:1e5,nbTopCandidates:5,charThreshold:500,keepClasses:!0,...r}).parse();if(!u||!u.content)return V(e,{fragment:!0});const p=V(u.content,{fragment:!0}),l={title:u.title,byline:u.byline,excerpt:u.excerpt,siteName:u.siteName,lang:u.lang};return o&&(o.data=o.data||{},o.data.readability=l),p&&(p.data=p.data||{},p.data.readability=l),p}},J={name:"readability",plugin:G,stage:0,main:!0},R={name:"restore-readability-meta",plugin:()=>(t,e)=>{e.data?.readability&&(t.data=t.data||{},t.data.readability=e.data.readability)},stage:0,after:"rehype-remark"},K=[J,R];export{r as DefaultPipelineStage,C as MdastBasePipeline,I as MdastPipeline,e as PipelineStage,z as astCompiler,H as astFormat,N as htmlFormat,G as htmlReadability,J as htmlReadabilityPlugin,K as htmlReadabilityPlugins,$ as jsonParser,j as markdownFormat,L as mdast,R as restoreReadabilityMetaPlugin};
|
package/docs/README.md
CHANGED
|
@@ -93,6 +93,8 @@ console.log(vfile.value); // The serialized HTML string
|
|
|
93
93
|
|
|
94
94
|
`mdast-plus` uses [unified](https://github.com/unifiedjs/unified) internally. If you add the same plugin function multiple times, the last configuration **overrides** the previous ones.
|
|
95
95
|
|
|
96
|
+
> **Warning**: Passing `false` as a plugin option (e.g., `.use(myPlugin, false)`) will **disable** the plugin entirely. For regular plugins, this means they simply won't run. For plugins marked as `main: true` (like replacements for the default parser), if they are disabled with `false`, they will **not** replace the default plugin of that stage, providing a safe fallback to the default behavior. If you want to bypass a plugin's logic while keeping it active (e.g. to maintain its parser), use an options object like `{ enable: false }` instead.
|
|
97
|
+
|
|
96
98
|
```typescript
|
|
97
99
|
// The plugin will run ONCE with option: 2
|
|
98
100
|
pipeline.use(myPlugin, { option: 1 });
|
|
@@ -82,6 +82,8 @@ export const myPlugin: MdastPlugin = {
|
|
|
82
82
|
};
|
|
83
83
|
```
|
|
84
84
|
|
|
85
|
+
> **Important**: Never pass `false` as the second argument to `.use()` or within `MdastPlugin.options` if you want the plugin to execute. In `unified`, `false` is a special value that **disables** the plugin. If your plugin is a "main" plugin (like a parser replacement), disabling it with `false` will prevent it from replacing the default plugin of that stage, providing a safe fallback. A warning will be printed to the terminal when this fallback occurs. Use an options object (e.g., `{ enabled: false }`) if you need a way to skip logic within the plugin itself while keeping it active.
|
|
86
|
+
|
|
85
87
|
### Main Plugins
|
|
86
88
|
|
|
87
89
|
Each stage supports a **single** main plugin. When a plugin is marked with `main: true`, it will replace the first plugin that was originally registered for that stage. This is primarily used by formats to allow users to override the default parser or stringifier by injecting a different one at the same stage.
|
package/docs/_media/README.cn.md
CHANGED
|
@@ -89,6 +89,8 @@ console.log(vfile.value); // 序列化后的 HTML 字符串
|
|
|
89
89
|
|
|
90
90
|
`mdast-plus` 内部使用 [unified](https://github.com/unifiedjs/unified)。如果您多次添加同一个插件函数,最后的配置将**覆盖**之前的配置。
|
|
91
91
|
|
|
92
|
+
> **警告**: 将 `false` 作为插件选项传递(例如 `.use(myPlugin, false)`)将**完全禁用**该插件。对于普通插件,这意味着它们将不会运行。对于标记为 `main: true` 的插件(如默认解析器的替代者),如果它们被 `false` 禁用,它们将**不会**替换该阶段的默认插件,从而安全地回退到默认行为。如果你需要跳过插件逻辑但保持其激活状态(例如为了保留其解析器),请改用选项对象,例如 `{ enable: false }`。
|
|
93
|
+
|
|
92
94
|
```typescript
|
|
93
95
|
// 插件将只执行一次,且选项为: 2
|
|
94
96
|
pipeline.use(myPlugin, { option: 1 });
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
# Class: MdastBasePipeline
|
|
8
8
|
|
|
9
|
-
Defined in: [packages/mdast-plus/src/pipeline.ts:26](https://github.com/isdk/mdast-plus.js/blob/
|
|
9
|
+
Defined in: [packages/mdast-plus/src/pipeline.ts:26](https://github.com/isdk/mdast-plus.js/blob/becb4bdeb4b577bd75c7751367751e84056ff87e/src/pipeline.ts#L26)
|
|
10
10
|
|
|
11
11
|
Base implementation of the fluent mdast processing pipeline.
|
|
12
12
|
Manages the plugin registry and the execution queue.
|
|
@@ -21,7 +21,7 @@ Manages the plugin registry and the execution queue.
|
|
|
21
21
|
|
|
22
22
|
> **new MdastBasePipeline**(`input`): `MdastBasePipeline`
|
|
23
23
|
|
|
24
|
-
Defined in: [packages/mdast-plus/src/pipeline.ts:54](https://github.com/isdk/mdast-plus.js/blob/
|
|
24
|
+
Defined in: [packages/mdast-plus/src/pipeline.ts:54](https://github.com/isdk/mdast-plus.js/blob/becb4bdeb4b577bd75c7751367751e84056ff87e/src/pipeline.ts#L54)
|
|
25
25
|
|
|
26
26
|
Initializes a new pipeline instance with the given input.
|
|
27
27
|
|
|
@@ -43,7 +43,7 @@ Content to process (string, Buffer, VFile, or AST Node).
|
|
|
43
43
|
|
|
44
44
|
> `protected` **\_data**: `Record`\<`string`, `any`\> = `{}`
|
|
45
45
|
|
|
46
|
-
Defined in: [packages/mdast-plus/src/pipeline.ts:48](https://github.com/isdk/mdast-plus.js/blob/
|
|
46
|
+
Defined in: [packages/mdast-plus/src/pipeline.ts:48](https://github.com/isdk/mdast-plus.js/blob/becb4bdeb4b577bd75c7751367751e84056ff87e/src/pipeline.ts#L48)
|
|
47
47
|
|
|
48
48
|
***
|
|
49
49
|
|
|
@@ -51,7 +51,7 @@ Defined in: [packages/mdast-plus/src/pipeline.ts:48](https://github.com/isdk/mda
|
|
|
51
51
|
|
|
52
52
|
> `protected` **input**: `Compatible`
|
|
53
53
|
|
|
54
|
-
Defined in: [packages/mdast-plus/src/pipeline.ts:46](https://github.com/isdk/mdast-plus.js/blob/
|
|
54
|
+
Defined in: [packages/mdast-plus/src/pipeline.ts:46](https://github.com/isdk/mdast-plus.js/blob/becb4bdeb4b577bd75c7751367751e84056ff87e/src/pipeline.ts#L46)
|
|
55
55
|
|
|
56
56
|
***
|
|
57
57
|
|
|
@@ -59,7 +59,7 @@ Defined in: [packages/mdast-plus/src/pipeline.ts:46](https://github.com/isdk/mda
|
|
|
59
59
|
|
|
60
60
|
> `protected` **queue**: [`MdastPlugin`](../interfaces/MdastPlugin.md)[] = `[]`
|
|
61
61
|
|
|
62
|
-
Defined in: [packages/mdast-plus/src/pipeline.ts:47](https://github.com/isdk/mdast-plus.js/blob/
|
|
62
|
+
Defined in: [packages/mdast-plus/src/pipeline.ts:47](https://github.com/isdk/mdast-plus.js/blob/becb4bdeb4b577bd75c7751367751e84056ff87e/src/pipeline.ts#L47)
|
|
63
63
|
|
|
64
64
|
## Methods
|
|
65
65
|
|
|
@@ -67,7 +67,7 @@ Defined in: [packages/mdast-plus/src/pipeline.ts:47](https://github.com/isdk/mda
|
|
|
67
67
|
|
|
68
68
|
> `protected` **assembleProcessor**(`queue`): `Processor`
|
|
69
69
|
|
|
70
|
-
Defined in: [packages/mdast-plus/src/pipeline.ts:
|
|
70
|
+
Defined in: [packages/mdast-plus/src/pipeline.ts:347](https://github.com/isdk/mdast-plus.js/blob/becb4bdeb4b577bd75c7751367751e84056ff87e/src/pipeline.ts#L347)
|
|
71
71
|
|
|
72
72
|
Assembles a unified processor based on the sorted plugin queue.
|
|
73
73
|
|
|
@@ -83,11 +83,43 @@ Assembles a unified processor based on the sorted plugin queue.
|
|
|
83
83
|
|
|
84
84
|
***
|
|
85
85
|
|
|
86
|
+
### configure()
|
|
87
|
+
|
|
88
|
+
> **configure**(`pluginName`, ...`options`): `this`
|
|
89
|
+
|
|
90
|
+
Defined in: [packages/mdast-plus/src/pipeline.ts:331](https://github.com/isdk/mdast-plus.js/blob/becb4bdeb4b577bd75c7751367751e84056ff87e/src/pipeline.ts#L331)
|
|
91
|
+
|
|
92
|
+
Modifies the options of a plugin that is already in the pipeline queue.
|
|
93
|
+
Searches from the end of the queue and updates the first match found.
|
|
94
|
+
|
|
95
|
+
#### Parameters
|
|
96
|
+
|
|
97
|
+
##### pluginName
|
|
98
|
+
|
|
99
|
+
`string`
|
|
100
|
+
|
|
101
|
+
The name of the plugin to modify.
|
|
102
|
+
Matches against explicit plugin name or function name.
|
|
103
|
+
|
|
104
|
+
##### options
|
|
105
|
+
|
|
106
|
+
...`any`[]
|
|
107
|
+
|
|
108
|
+
The new options to pass to the plugin (replaces existing options).
|
|
109
|
+
|
|
110
|
+
#### Returns
|
|
111
|
+
|
|
112
|
+
`this`
|
|
113
|
+
|
|
114
|
+
The pipeline instance for chaining.
|
|
115
|
+
|
|
116
|
+
***
|
|
117
|
+
|
|
86
118
|
### data()
|
|
87
119
|
|
|
88
120
|
> **data**(`key`, `value?`): `this`
|
|
89
121
|
|
|
90
|
-
Defined in: [packages/mdast-plus/src/pipeline.ts:64](https://github.com/isdk/mdast-plus.js/blob/
|
|
122
|
+
Defined in: [packages/mdast-plus/src/pipeline.ts:64](https://github.com/isdk/mdast-plus.js/blob/becb4bdeb4b577bd75c7751367751e84056ff87e/src/pipeline.ts#L64)
|
|
91
123
|
|
|
92
124
|
Configures global data for the pipeline, which will be available to all plugins via this.data().
|
|
93
125
|
|
|
@@ -117,7 +149,7 @@ The pipeline instance for chaining.
|
|
|
117
149
|
|
|
118
150
|
> `protected` **ensureInputPlugins**(`queue`, `overrides?`, `maxStage?`): `void`
|
|
119
151
|
|
|
120
|
-
Defined in: [packages/mdast-plus/src/pipeline.ts:149](https://github.com/isdk/mdast-plus.js/blob/
|
|
152
|
+
Defined in: [packages/mdast-plus/src/pipeline.ts:149](https://github.com/isdk/mdast-plus.js/blob/becb4bdeb4b577bd75c7751367751e84056ff87e/src/pipeline.ts#L149)
|
|
121
153
|
|
|
122
154
|
Ensures that input plugins (parser, normalizers) are present in the queue.
|
|
123
155
|
Adds implicit plugins if no parser is detected.
|
|
@@ -146,7 +178,7 @@ Adds implicit plugins if no parser is detected.
|
|
|
146
178
|
|
|
147
179
|
> **from**(`fmt`, `overrides?`): `this`
|
|
148
180
|
|
|
149
|
-
Defined in: [packages/mdast-plus/src/pipeline.ts:178](https://github.com/isdk/mdast-plus.js/blob/
|
|
181
|
+
Defined in: [packages/mdast-plus/src/pipeline.ts:178](https://github.com/isdk/mdast-plus.js/blob/becb4bdeb4b577bd75c7751367751e84056ff87e/src/pipeline.ts#L178)
|
|
150
182
|
|
|
151
183
|
Configures the input format and adds its associated plugins to the pipeline.
|
|
152
184
|
|
|
@@ -176,7 +208,7 @@ The pipeline instance for chaining.
|
|
|
176
208
|
|
|
177
209
|
> **getFormat**(`id`): `undefined` \| [`MdastFormat`](../interfaces/MdastFormat.md)
|
|
178
210
|
|
|
179
|
-
Defined in: [packages/mdast-plus/src/pipeline.ts:76](https://github.com/isdk/mdast-plus.js/blob/
|
|
211
|
+
Defined in: [packages/mdast-plus/src/pipeline.ts:76](https://github.com/isdk/mdast-plus.js/blob/becb4bdeb4b577bd75c7751367751e84056ff87e/src/pipeline.ts#L76)
|
|
180
212
|
|
|
181
213
|
Instance-level access to the global format registry.
|
|
182
214
|
|
|
@@ -196,7 +228,7 @@ Instance-level access to the global format registry.
|
|
|
196
228
|
|
|
197
229
|
> **priority**(`order`): `this`
|
|
198
230
|
|
|
199
|
-
Defined in: [packages/mdast-plus/src/pipeline.ts:314](https://github.com/isdk/mdast-plus.js/blob/
|
|
231
|
+
Defined in: [packages/mdast-plus/src/pipeline.ts:314](https://github.com/isdk/mdast-plus.js/blob/becb4bdeb4b577bd75c7751367751e84056ff87e/src/pipeline.ts#L314)
|
|
200
232
|
|
|
201
233
|
Sets the priority order for the most recently added plugin.
|
|
202
234
|
Plugins with lower order run earlier within the same stage.
|
|
@@ -221,7 +253,7 @@ The pipeline instance for chaining.
|
|
|
221
253
|
|
|
222
254
|
> **to**(`fmt`, `overrides?`): `Promise`\<`VFile`\>
|
|
223
255
|
|
|
224
|
-
Defined in: [packages/mdast-plus/src/pipeline.ts:198](https://github.com/isdk/mdast-plus.js/blob/
|
|
256
|
+
Defined in: [packages/mdast-plus/src/pipeline.ts:198](https://github.com/isdk/mdast-plus.js/blob/becb4bdeb4b577bd75c7751367751e84056ff87e/src/pipeline.ts#L198)
|
|
225
257
|
|
|
226
258
|
Processes the pipeline and serializes the result into the specified format.
|
|
227
259
|
|
|
@@ -251,7 +283,7 @@ A promise resolving to a VFile containing the result.
|
|
|
251
283
|
|
|
252
284
|
> `protected` **toRuntimeEntry**(`entry`, `defaultStage`, `overrides?`): [`MdastPlugin`](../interfaces/MdastPlugin.md) & `object`
|
|
253
285
|
|
|
254
|
-
Defined in: [packages/mdast-plus/src/pipeline.ts:100](https://github.com/isdk/mdast-plus.js/blob/
|
|
286
|
+
Defined in: [packages/mdast-plus/src/pipeline.ts:100](https://github.com/isdk/mdast-plus.js/blob/becb4bdeb4b577bd75c7751367751e84056ff87e/src/pipeline.ts#L100)
|
|
255
287
|
|
|
256
288
|
Normalizes a plugin entry for runtime execution.
|
|
257
289
|
|
|
@@ -279,7 +311,7 @@ Normalizes a plugin entry for runtime execution.
|
|
|
279
311
|
|
|
280
312
|
> **use**(`plugin`, ...`options`): `this`
|
|
281
313
|
|
|
282
|
-
Defined in: [packages/mdast-plus/src/pipeline.ts:241](https://github.com/isdk/mdast-plus.js/blob/
|
|
314
|
+
Defined in: [packages/mdast-plus/src/pipeline.ts:241](https://github.com/isdk/mdast-plus.js/blob/becb4bdeb4b577bd75c7751367751e84056ff87e/src/pipeline.ts#L241)
|
|
283
315
|
|
|
284
316
|
Adds a plugin or an array of plugins to the pipeline's compile stage.
|
|
285
317
|
|
|
@@ -311,7 +343,7 @@ The pipeline instance for chaining.
|
|
|
311
343
|
|
|
312
344
|
> **useAt**(`stage`, `plugin`, ...`options`): `this`
|
|
313
345
|
|
|
314
|
-
Defined in: [packages/mdast-plus/src/pipeline.ts:252](https://github.com/isdk/mdast-plus.js/blob/
|
|
346
|
+
Defined in: [packages/mdast-plus/src/pipeline.ts:252](https://github.com/isdk/mdast-plus.js/blob/becb4bdeb4b577bd75c7751367751e84056ff87e/src/pipeline.ts#L252)
|
|
315
347
|
|
|
316
348
|
Adds a plugin or an array of plugins to the pipeline at a specific stage.
|
|
317
349
|
|
|
@@ -321,7 +353,7 @@ Adds a plugin or an array of plugins to the pipeline at a specific stage.
|
|
|
321
353
|
|
|
322
354
|
The stage name or numeric value.
|
|
323
355
|
|
|
324
|
-
`"parse"` | `"normalize"` | `"compile"` | `"finalize"` | `"stringify"`
|
|
356
|
+
[`PipelineStage`](../enumerations/PipelineStage.md) | `"parse"` | `"normalize"` | `"compile"` | `"finalize"` | `"stringify"`
|
|
325
357
|
|
|
326
358
|
###### plugin
|
|
327
359
|
|
|
@@ -345,7 +377,7 @@ The pipeline instance for chaining.
|
|
|
345
377
|
|
|
346
378
|
> **useAt**(`plugin`, ...`options`): `this`
|
|
347
379
|
|
|
348
|
-
Defined in: [packages/mdast-plus/src/pipeline.ts:259](https://github.com/isdk/mdast-plus.js/blob/
|
|
380
|
+
Defined in: [packages/mdast-plus/src/pipeline.ts:259](https://github.com/isdk/mdast-plus.js/blob/becb4bdeb4b577bd75c7751367751e84056ff87e/src/pipeline.ts#L259)
|
|
349
381
|
|
|
350
382
|
Adds a plugin or an array of plugins to the pipeline. The stage is taken from the plugin object(s).
|
|
351
383
|
|
|
@@ -355,7 +387,7 @@ Adds a plugin or an array of plugins to the pipeline. The stage is taken from th
|
|
|
355
387
|
|
|
356
388
|
The MdastPlugin object or an array of them.
|
|
357
389
|
|
|
358
|
-
[`MdastPlugin`](../interfaces/MdastPlugin.md) |
|
|
390
|
+
[`MdastPlugin`](../interfaces/MdastPlugin.md) | [`MdastPlugin`](../interfaces/MdastPlugin.md)[]
|
|
359
391
|
|
|
360
392
|
###### options
|
|
361
393
|
|
|
@@ -375,7 +407,7 @@ The pipeline instance for chaining.
|
|
|
375
407
|
|
|
376
408
|
> `static` **getFormat**(`id`): `undefined` \| [`MdastFormat`](../interfaces/MdastFormat.md)
|
|
377
409
|
|
|
378
|
-
Defined in: [packages/mdast-plus/src/pipeline.ts:42](https://github.com/isdk/mdast-plus.js/blob/
|
|
410
|
+
Defined in: [packages/mdast-plus/src/pipeline.ts:42](https://github.com/isdk/mdast-plus.js/blob/becb4bdeb4b577bd75c7751367751e84056ff87e/src/pipeline.ts#L42)
|
|
379
411
|
|
|
380
412
|
Retrieves a registered format by its ID.
|
|
381
413
|
|
|
@@ -399,7 +431,7 @@ The format definition or undefined if not found.
|
|
|
399
431
|
|
|
400
432
|
> `static` **register**(`format`): `void`
|
|
401
433
|
|
|
402
|
-
Defined in: [packages/mdast-plus/src/pipeline.ts:33](https://github.com/isdk/mdast-plus.js/blob/
|
|
434
|
+
Defined in: [packages/mdast-plus/src/pipeline.ts:33](https://github.com/isdk/mdast-plus.js/blob/becb4bdeb4b577bd75c7751367751e84056ff87e/src/pipeline.ts#L33)
|
|
403
435
|
|
|
404
436
|
Registers a global document format.
|
|
405
437
|
|