down-parse
Advanced tools
Comparing version 2.2.0 to 2.3.0
@@ -1,1 +0,1 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var PluginCtx=function(){var n=this;this.renders=[],this.parsers=[],this.use=function(e){var t=e.parser,r=e.render;t&&n.parsers.push(t),r&&n.renders.push(r)},this.applyParses=function(e,r){return n.parsers.reduce(function(e,t){return t(e,r)},e)},this.applyRenders=function(r,e){return n.renders.reduce(function(e,t){return t(r,e)},e)}},ctx=new PluginCtx,use=ctx.use;function createPlugin(e){return e}function getTokenFrom(e){for(var t=[],r=0;r<e.length;r++){var n=e[r],i=void 0;if(n.startsWith("#")){var s=n.split("#"),o=s.pop();o||(o=""),i={type:"#",weight:s.length,text:o.trim(),origin:n}}else if(n.startsWith("---")||n.startsWith("***"))i={type:"hr",origin:n};else if(/^[>-]|\* /.test(n)||/^[0-9]. /.test(n)){var p=n.split(""),l=p[0],a=p.slice(1);if(Number.isNaN(+l))"-"===l&&(l="*"),i={type:l,inner:getTokenFrom([a.slice(1).join("")])[0],n:0,origin:n};else i={type:0,n:+l,inner:getTokenFrom([a.slice(2).join("")])[0],origin:n}}else if(n.startsWith("```")){var u=n.split(" ").slice(1),c=e.slice(r+1),f=c.findIndex(function(e){return"```"===e});r=r+f+1,i={type:"</>",params:u,code:c.slice(0,f).join("\n"),origin:n}}else i=""===n?{type:"br",origin:n}:{type:"p",text:n,origin:n};t.push(ctx.applyParses(i,n))}return t}function map2lines(e){return e.split("\n").map(function(e){return e.trim()})}function parse(e){return getTokenFrom(map2lines(e))}function fold(e){var t=e[0],r=e.slice(1);if(void 0===t)return[];if("br"===t.type||"#"===t.type||"</>"===t.type||"p"===t.type||"hr"===t.type)return[t].concat(fold(r));for(var n=[],i=0;i<r.length;i++){var s=r[i];if(t.type!==s.type)break;n.push(s)}return[{type:t.type,block:fold([t].concat(n).map(function(e){return e.inner}))}].concat(fold(r.slice(n.length)))}function compile(e){return fold(parse(e))}function astEval(e){return e.reduce(function(e,t){var r;if("p"===t.type)r="<p>"+t.text+"</p>";else if("br"===t.type)r="<br />";else if("#"===t.type){var n=t.weight;r="<h"+n+">"+t.text+"</h"+n+">"}else if("</>"===t.type)r='<pre><code class="'+t.params.join(" ")+'">'+t.code+"</code></pre>";else if(0===t.type){var i=astEval(t.block);r='<ol type="'+t.type+'">'+i+"</ol>"}else if("hr"===t.type)r="<hr />";else{i=astEval(t.block);r='<ul type="'+t.type+'">'+i+"</ul>"}return e+ctx.applyRenders(t,r)},"")}function render(e){return astEval(compile(e))}exports.fold=fold,exports.compile=compile,exports.astEval=astEval,exports.render=render,exports.getTokenFrom=getTokenFrom,exports.parse=parse,exports.PluginCtx=PluginCtx,exports.ctx=ctx,exports.use=use,exports.createPlugin=createPlugin; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var PluginCtx=function(){var s=this;this.renders=[],this.parsers=[],this.afterParsers=[],this.use=function(e){var r=e.parser,t=e.render,n=e.afterParser;r&&s.parsers.push(r),t&&s.renders.push(t),n&&s.afterParsers.push(n)},this.applyParses=function(e,t){return s.parsers.reduce(function(e,r){return r(e,t)},e)},this.applyRenders=function(t,e){return s.renders.reduce(function(e,r){return r(t,e)},e)},this.applyAfterParsers=function(r){var t=function(e){return r=e};return s.afterParsers.forEach(function(e){e(r,t)}),r}},ctx=new PluginCtx,use=ctx.use;function createPlugin(e){return e}function getTokenFrom(e){for(var r=[],t=0;t<e.length;t++){var n=e[t],s=void 0;if(n.startsWith("#")){var i=n.split("#"),o=i.pop();o||(o=""),s={type:"#",weight:i.length,text:o.trim(),origin:n}}else if(n.startsWith("---")||n.startsWith("***"))s={type:"hr",origin:n};else if(/^[>-]|\* /.test(n)||/^[0-9]. /.test(n)){var p=n.split(""),a=p[0],l=p.slice(1);if(Number.isNaN(+a))"-"===a&&(a="*"),s={type:a,inner:getTokenFrom([l.slice(1).join("")])[0],n:0,origin:n};else s={type:0,n:+a,inner:getTokenFrom([l.slice(2).join("")])[0],origin:n}}else if(n.startsWith("```")){var u=n.split(" ").slice(1),c=e.slice(t+1),f=c.findIndex(function(e){return"```"===e});t=t+f+1,s={type:"</>",params:u,code:c.slice(0,f).join("\n"),origin:n}}else s=""===n?{type:"br",origin:n}:{type:"p",text:n,origin:n};r.push(ctx.applyParses(s,n))}return r}function map2lines(e){return e.split("\n").map(function(e){return e.trim()})}function parse(e){var r=getTokenFrom(map2lines(e));return ctx.applyAfterParsers(r)}function fold(e){var r=e[0],t=e.slice(1);if(void 0===r)return[];if("br"===r.type||"#"===r.type||"</>"===r.type||"p"===r.type||"hr"===r.type)return[r].concat(fold(t));for(var n=[],s=0;s<t.length;s++){var i=t[s];if(r.type!==i.type)break;n.push(i)}return[{type:r.type,block:fold([r].concat(n).map(function(e){return e.inner}))}].concat(fold(t.slice(n.length)))}function compile(e){return fold(parse(e))}function astEval(e){return e.reduce(function(e,r){var t;if("p"===r.type)t="<p>"+r.text+"</p>";else if("br"===r.type)t="<br />";else if("#"===r.type){var n=r.weight;t="<h"+n+">"+r.text+"</h"+n+">"}else if("</>"===r.type)t='<pre><code class="'+r.params.join(" ")+'">'+r.code+"</code></pre>";else if(0===r.type){var s=astEval(r.block);t='<ol type="'+r.type+'">'+s+"</ol>"}else if("hr"===r.type)t="<hr />";else{s=astEval(r.block);t='<ul type="'+r.type+'">'+s+"</ul>"}return e+ctx.applyRenders(r,t)},"")}function render(e){return astEval(compile(e))}exports.fold=fold,exports.compile=compile,exports.astEval=astEval,exports.render=render,exports.getTokenFrom=getTokenFrom,exports.parse=parse,exports.PluginCtx=PluginCtx,exports.ctx=ctx,exports.use=use,exports.createPlugin=createPlugin; |
export * from "./type"; | ||
import { Plugin, RenderMiddleWare, TokenMiddleWare } from "./type"; | ||
import { Plugin, RenderMiddleWare, TokenMiddleWare, AfterParser } from "./type"; | ||
import { Token } from "src/token"; | ||
/** | ||
@@ -9,5 +10,7 @@ * Plugin Module | ||
parsers: TokenMiddleWare[]; | ||
afterParsers: AfterParser[]; | ||
use: (plugin: Plugin) => void; | ||
applyParses: TokenMiddleWare; | ||
applyRenders: RenderMiddleWare; | ||
applyAfterParsers: (tokens: Token[]) => Token[]; | ||
} | ||
@@ -14,0 +17,0 @@ export declare const ctx: PluginCtx; |
import { Token } from "../token"; | ||
import { AST } from "../compile"; | ||
/** | ||
* It's a middleware for you to after-process the token, | ||
* it's a middleware for you to after-process the token, | ||
* @param token when parse '# 123', you will get { type: '#', weight: 1, text: '123' } | ||
@@ -10,9 +10,18 @@ * @param token when parse '# 123', the origin will be '# 123' | ||
/** | ||
* When return a string, it will replace | ||
* when return a string, it will replace | ||
* the default output of this AST_Node; | ||
* When return false, runner will skip it; | ||
* when return false, runner will skip it; | ||
* @param AST_Node when parse '# 123' you will get 'HeaderAST' | ||
*/ | ||
export declare type RenderMiddleWare = (AST_Node: AST, output: string) => string; | ||
export declare type AfterParser = ( | ||
/** | ||
* The Tokens Parsed | ||
*/ | ||
tokens: Token[], | ||
/** | ||
* you can replace the tokens by RUNNING setTokens | ||
*/ | ||
setTokens: (newTokens: Token[]) => void) => void; | ||
/** | ||
* Your Plugin Class Should Implement This | ||
@@ -23,2 +32,3 @@ */ | ||
render?: RenderMiddleWare; | ||
afterParser?: AfterParser; | ||
} |
@@ -24,7 +24,10 @@ export declare type GeneralToken<T, Parameter> = { | ||
/** | ||
* 制造 Token | ||
* get tokens from lines (string[]) | ||
* @param lineOne | ||
* @param WhichLine | ||
*/ | ||
export declare function getTokenFrom(lines: string[]): Token[]; | ||
/** | ||
* parse text to tokens | ||
* @param text the markdown text | ||
*/ | ||
export declare function parse(text: string): Token[]; |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t(e.DownParse={})}(this,function(e){"use strict";var t=function(){var n=this;this.renders=[],this.parsers=[],this.use=function(e){var t=e.parser,r=e.render;t&&n.parsers.push(t),r&&n.renders.push(r)},this.applyParses=function(e,r){return n.parsers.reduce(function(e,t){return t(e,r)},e)},this.applyRenders=function(r,e){return n.renders.reduce(function(e,t){return t(r,e)},e)}},y=new t,r=y.use;function d(e){for(var t=[],r=0;r<e.length;r++){var n=e[r],i=void 0;if(n.startsWith("#")){var s=n.split("#"),p=s.pop();p||(p=""),i={type:"#",weight:s.length,text:p.trim(),origin:n}}else if(n.startsWith("---")||n.startsWith("***"))i={type:"hr",origin:n};else if(/^[>-]|\* /.test(n)||/^[0-9]. /.test(n)){var o=n.split(""),u=o[0],c=o.slice(1);if(Number.isNaN(+u))"-"===u&&(u="*"),i={type:u,inner:d([c.slice(1).join("")])[0],n:0,origin:n};else i={type:0,n:+u,inner:d([c.slice(2).join("")])[0],origin:n}}else if(n.startsWith("```")){var a=n.split(" ").slice(1),l=e.slice(r+1),f=l.findIndex(function(e){return"```"===e});r=r+f+1,i={type:"</>",params:a,code:l.slice(0,f).join("\n"),origin:n}}else i=""===n?{type:"br",origin:n}:{type:"p",text:n,origin:n};t.push(y.applyParses(i,n))}return t}function n(e){return d(e.split("\n").map(function(e){return e.trim()}))}function p(e){var t=e[0],r=e.slice(1);if(void 0===t)return[];if("br"===t.type||"#"===t.type||"</>"===t.type||"p"===t.type||"hr"===t.type)return[t].concat(p(r));for(var n=[],i=0;i<r.length;i++){var s=r[i];if(t.type!==s.type)break;n.push(s)}return[{type:t.type,block:p([t].concat(n).map(function(e){return e.inner}))}].concat(p(r.slice(n.length)))}function i(e){return p(n(e))}function s(e){return e.reduce(function(e,t){var r;if("p"===t.type)r="<p>"+t.text+"</p>";else if("br"===t.type)r="<br />";else if("#"===t.type){var n=t.weight;r="<h"+n+">"+t.text+"</h"+n+">"}else if("</>"===t.type)r='<pre><code class="'+t.params.join(" ")+'">'+t.code+"</code></pre>";else if(0===t.type){var i=s(t.block);r='<ol type="'+t.type+'">'+i+"</ol>"}else if("hr"===t.type)r="<hr />";else{i=s(t.block);r='<ul type="'+t.type+'">'+i+"</ul>"}return e+y.applyRenders(t,r)},"")}e.fold=p,e.compile=i,e.astEval=s,e.render=function(e){return s(i(e))},e.getTokenFrom=d,e.parse=n,e.PluginCtx=t,e.ctx=y,e.use=r,e.createPlugin=function(e){return e},Object.defineProperty(e,"__esModule",{value:!0})}); | ||
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):r(e.DownParse={})}(this,function(e){"use strict";var r=function(){var i=this;this.renders=[],this.parsers=[],this.afterParsers=[],this.use=function(e){var r=e.parser,t=e.render,n=e.afterParser;r&&i.parsers.push(r),t&&i.renders.push(t),n&&i.afterParsers.push(n)},this.applyParses=function(e,t){return i.parsers.reduce(function(e,r){return r(e,t)},e)},this.applyRenders=function(t,e){return i.renders.reduce(function(e,r){return r(t,e)},e)},this.applyAfterParsers=function(r){var t=function(e){return r=e};return i.afterParsers.forEach(function(e){e(r,t)}),r}},y=new r,t=y.use;function h(e){for(var r=[],t=0;t<e.length;t++){var n=e[t],i=void 0;if(n.startsWith("#")){var s=n.split("#"),p=s.pop();p||(p=""),i={type:"#",weight:s.length,text:p.trim(),origin:n}}else if(n.startsWith("---")||n.startsWith("***"))i={type:"hr",origin:n};else if(/^[>-]|\* /.test(n)||/^[0-9]. /.test(n)){var o=n.split(""),u=o[0],a=o.slice(1);if(Number.isNaN(+u))"-"===u&&(u="*"),i={type:u,inner:h([a.slice(1).join("")])[0],n:0,origin:n};else i={type:0,n:+u,inner:h([a.slice(2).join("")])[0],origin:n}}else if(n.startsWith("```")){var f=n.split(" ").slice(1),c=e.slice(t+1),l=c.findIndex(function(e){return"```"===e});t=t+l+1,i={type:"</>",params:f,code:c.slice(0,l).join("\n"),origin:n}}else i=""===n?{type:"br",origin:n}:{type:"p",text:n,origin:n};r.push(y.applyParses(i,n))}return r}function n(e){var r=h(e.split("\n").map(function(e){return e.trim()}));return y.applyAfterParsers(r)}function p(e){var r=e[0],t=e.slice(1);if(void 0===r)return[];if("br"===r.type||"#"===r.type||"</>"===r.type||"p"===r.type||"hr"===r.type)return[r].concat(p(t));for(var n=[],i=0;i<t.length;i++){var s=t[i];if(r.type!==s.type)break;n.push(s)}return[{type:r.type,block:p([r].concat(n).map(function(e){return e.inner}))}].concat(p(t.slice(n.length)))}function i(e){return p(n(e))}function s(e){return e.reduce(function(e,r){var t;if("p"===r.type)t="<p>"+r.text+"</p>";else if("br"===r.type)t="<br />";else if("#"===r.type){var n=r.weight;t="<h"+n+">"+r.text+"</h"+n+">"}else if("</>"===r.type)t='<pre><code class="'+r.params.join(" ")+'">'+r.code+"</code></pre>";else if(0===r.type){var i=s(r.block);t='<ol type="'+r.type+'">'+i+"</ol>"}else if("hr"===r.type)t="<hr />";else{i=s(r.block);t='<ul type="'+r.type+'">'+i+"</ul>"}return e+y.applyRenders(r,t)},"")}e.fold=p,e.compile=i,e.astEval=s,e.render=function(e){return s(i(e))},e.getTokenFrom=h,e.parse=n,e.PluginCtx=r,e.ctx=y,e.use=t,e.createPlugin=function(e){return e},Object.defineProperty(e,"__esModule",{value:!0})}); |
{ | ||
"name": "down-parse", | ||
"version": "2.2.0", | ||
"version": "2.3.0", | ||
"description": "markdown parser", | ||
@@ -5,0 +5,0 @@ "main": "dist/main.js", |
export * from "./type"; | ||
import { Plugin, RenderMiddleWare, TokenMiddleWare } from "./type"; | ||
import { Plugin, RenderMiddleWare, TokenMiddleWare, AfterParser } from "./type"; | ||
import { Token } from "src/token"; | ||
/** | ||
@@ -15,5 +17,8 @@ * Plugin Module | ||
// AfterParsers MiddleWare | ||
afterParsers: AfterParser[] = []; | ||
// Install A Plugin | ||
use = (plugin: Plugin) => { | ||
const { parser, render } = plugin; | ||
const { parser, render, afterParser } = plugin; | ||
@@ -27,2 +32,6 @@ if (parser) { | ||
} | ||
if (afterParser) { | ||
this.afterParsers.push(afterParser); | ||
} | ||
} | ||
@@ -43,2 +52,13 @@ | ||
} | ||
// Apply AfterParser | ||
applyAfterParsers = (tokens: Token[]) => { | ||
const setToken = (newTokens: Token[]) => tokens = newTokens; | ||
this.afterParsers.forEach(afterParser => { | ||
afterParser(tokens, setToken); | ||
}); | ||
return tokens; | ||
} | ||
} | ||
@@ -45,0 +65,0 @@ |
@@ -6,3 +6,3 @@ import { Token } from "../token"; | ||
/** | ||
* It's a middleware for you to after-process the token, | ||
* it's a middleware for you to after-process the token, | ||
* @param token when parse '# 123', you will get { type: '#', weight: 1, text: '123' } | ||
@@ -14,5 +14,5 @@ * @param token when parse '# 123', the origin will be '# 123' | ||
/** | ||
* When return a string, it will replace | ||
* when return a string, it will replace | ||
* the default output of this AST_Node; | ||
* When return false, runner will skip it; | ||
* when return false, runner will skip it; | ||
* @param AST_Node when parse '# 123' you will get 'HeaderAST' | ||
@@ -22,2 +22,14 @@ */ | ||
export type AfterParser = ( | ||
/** | ||
* The Tokens Parsed | ||
*/ | ||
tokens: Token[], | ||
/** | ||
* you can replace the tokens by RUNNING setTokens | ||
*/ | ||
setTokens: (newTokens: Token[]) => void | ||
) => void; | ||
/** | ||
@@ -27,4 +39,10 @@ * Your Plugin Class Should Implement This | ||
export interface Plugin { | ||
// the parser will be INVOKED one by one | ||
parser?: TokenMiddleWare, | ||
render?: RenderMiddleWare | ||
// the render will be INVOKED one by one | ||
render?: RenderMiddleWare, | ||
// the function invoked when down-parse finish token parse | ||
afterParser?: AfterParser | ||
} |
@@ -34,8 +34,6 @@ import { ctx } from "./plugin"; | ||
/** | ||
* 制造 Token | ||
* get tokens from lines (string[]) | ||
* @param lineOne | ||
* @param WhichLine | ||
*/ | ||
export function getTokenFrom(lines: string[]): Token[] { | ||
// lineOne: string, WhichLine: number | ||
const tokens: Token[] = []; | ||
@@ -119,5 +117,12 @@ for (let i = 0; i < lines.length; i ++) { | ||
/** | ||
* parse text to tokens | ||
* @param text the markdown text | ||
*/ | ||
export function parse(text: string) { | ||
const lines = map2lines(text);; | ||
return getTokenFrom(lines); | ||
const tokens = getTokenFrom(lines); | ||
return ctx.applyAfterParsers(tokens); | ||
} |
43
test.js
const { render, use } = require('./dist/dev'); | ||
use({ | ||
parser(token) { | ||
// Change The ParaAST's Property To Uppercase. | ||
if (token.type === 'p') { | ||
const { text } = token; | ||
token.text = text.toUpperCase(); | ||
afterParser(tokens, setTokens) { | ||
const newTokens = []; | ||
for (let i = 0; i < tokens.length - 1; i ++) { | ||
const now = tokens[i]; | ||
const next = tokens[i + 1]; | ||
if (now.type === 'br') { | ||
if (next.type !== 'br') { | ||
newTokens.push(now); | ||
} | ||
} else { | ||
newTokens.push(now); | ||
} | ||
} | ||
// Don't Forget Return It. | ||
return token; | ||
}, | ||
render(ast, output) { | ||
if (ast.type === 'p') { | ||
return output.replace('WORLD', '😊'); | ||
} else { | ||
// Keep Default Output For Other AST. | ||
return output; | ||
} | ||
console.log(newTokens) | ||
setTokens(newTokens); | ||
} | ||
@@ -28,9 +27,13 @@ }); | ||
\`\`\` js | ||
console.log(!!!); | ||
\`\`\` | ||
pppp | ||
p | ||
`); | ||
console.log(res); | ||
console.log('res', res); | ||
// => '<br /><h1>I ❤️ Plugin</h1><p>HELLO, 😊</p><br />' |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25440
553