Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

down-parse

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

down-parse - npm Package Compare versions

Comparing version 2.3.1 to 2.4.0

2

dist/main.js

@@ -1,1 +0,1 @@

"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){t="<ol >"+astEval(r.block)+"</ol>"}else if("hr"===r.type)t="<hr />";else if("*"===r.type){t="<ul>"+astEval(r.block)+"</ul>"}else{t="<blockquote>"+astEval(r.block)+"</blockquote>"}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;
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var PluginCtx=function(){var i=this;this.renders=[],this.parsers=[],this.afterParsers=[],this.use=function(e){var t=e.parser,r=e.render,n=e.afterParser;t&&i.parsers.push(t),r&&i.renders.push(r),n&&i.afterParsers.push(n)},this.applyParses=function(e,r){return i.parsers.reduce(function(e,t){return t(e,r)},e)},this.applyRenders=function(r,e){return i.renders.reduce(function(e,t){return t(r,e)},e)},this.applyAfterParsers=function(t){var r=function(e){return t=e};return i.afterParsers.forEach(function(e){e(t,r)}),t}},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.endsWith("|")){var a=e.slice(r),l=0;for(l=0;l<a.length;l++){var p=a[l];if(!p.startsWith("|")||!p.endsWith("|"))break}r=r+l-1,i={type:"|",columns:a.slice(0,l).map(function(e){return e.split("|").slice(1,-1).map(function(e){return e.trim()})})}}else if(n.startsWith("---")||n.startsWith("***"))i={type:"hr",origin:n};else if(/^[>-]|\* /.test(n)||/^[0-9]. /.test(n)){var u=n.split(""),c=u[0],f=u.slice(1);if(Number.isNaN(+c))"-"===c&&(c="*"),i={type:c,inner:getTokenFrom([f.slice(1).join("")])[0],n:0,origin:n};else i={type:0,n:+c,inner:getTokenFrom([f.slice(2).join("")])[0],origin:n}}else if(n.startsWith("```")){var h=n.split(" ").slice(1),d=(a=e.slice(r+1)).findIndex(function(e){return"```"===e});r=r+d+1,i={type:"</>",params:h,code:a.slice(0,d).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){var t=getTokenFrom(map2lines(e));return ctx.applyAfterParsers(t)}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||"|"===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){r="<ol >"+astEval(t.block)+"</ol>"}else if("hr"===t.type)r="<hr />";else if("*"===t.type){r="<ul>"+astEval(t.block)+"</ul>"}else{if("|"===t.type){var i=t.columns,s=i[0],o=i[1]||[],a=i.slice(2),l=function(e){var t=o[e].trim();return{"::":"center"," :":"right",": ":"left"," ":"left"}[(":"===t[0]?":":" ")+(":"===t[t.length-1]?":":" ")]};return["<table>","<thead>"+("<tr>"+s.map(function(e,t){return'<th style="text-align: '+(l(t)||"")+'">'+e+"</th>"}).join("")+"</tr>")+"</thead>","<tbody>"+a.map(function(e){return"<tr>"+e.map(function(e,t){return'<td style="text-align: '+(l(t)||"")+'">'+e+"</td>"}).join("")+"</tr>"}).join("")+"</tbody>","</table>"].join("")}r="<blockquote>"+astEval(t.block)+"</blockquote>"}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;

@@ -1,2 +0,2 @@

import { Token, CodeToken, HeaderToken, ParaToken, BrToken, HrToken } from "./token";
import { Token, BrToken, HrToken, ParaToken, CodeToken, TableToken, HeaderToken } from "./token";
export declare type ParaAST = ParaToken;

@@ -7,2 +7,3 @@ export declare type BrAST = BrToken;

export declare type CodeAST = CodeToken;
export declare type TableAST = TableToken;
export declare type GeneralAST<T, Parameter> = {

@@ -14,3 +15,3 @@ type: T;

}>;
export declare type AST = ParaAST | BrAST | HrAST | BlockAST | HeaderAST | CodeAST;
export declare type AST = ParaAST | BrAST | HrAST | BlockAST | HeaderAST | CodeAST | TableAST;
/**

@@ -17,0 +18,0 @@ * 将 Token 转为嵌套的

@@ -20,5 +20,8 @@ export declare type GeneralToken<T, Parameter> = {

}>;
export declare type TableToken = GeneralToken<'|', {
columns: string[][];
}>;
export declare type HrToken = GeneralToken<'hr', {}>;
export declare type BrToken = GeneralToken<'br', {}>;
export declare type Token = HeaderToken | BrToken | HrToken | ParaToken | CodeToken | BlockToken;
export declare type Token = HeaderToken | BrToken | HrToken | ParaToken | CodeToken | BlockToken | TableToken;
/**

@@ -25,0 +28,0 @@ * get tokens from lines (string[])

@@ -1,1 +0,1 @@

!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("#"),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(""),u=p[0],a=p.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 c=n.split(" ").slice(1),f=e.slice(t+1),l=f.findIndex(function(e){return"```"===e});t=t+l+1,i={type:"</>",params:c,code:f.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 o(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(o(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:o([r].concat(n).map(function(e){return e.inner}))}].concat(o(t.slice(n.length)))}function i(e){return o(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){t="<ol >"+s(r.block)+"</ol>"}else if("hr"===r.type)t="<hr />";else if("*"===r.type){t="<ul>"+s(r.block)+"</ul>"}else{t="<blockquote>"+s(r.block)+"</blockquote>"}return e+y.applyRenders(r,t)},"")}e.fold=o,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})});
!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 i=this;this.renders=[],this.parsers=[],this.afterParsers=[],this.use=function(e){var t=e.parser,r=e.render,n=e.afterParser;t&&i.parsers.push(t),r&&i.renders.push(r),n&&i.afterParsers.push(n)},this.applyParses=function(e,r){return i.parsers.reduce(function(e,t){return t(e,r)},e)},this.applyRenders=function(r,e){return i.renders.reduce(function(e,t){return t(r,e)},e)},this.applyAfterParsers=function(t){var r=function(e){return t=e};return i.afterParsers.forEach(function(e){e(t,r)}),t}},d=new t,r=d.use;function v(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.endsWith("|")){var u=e.slice(r),a=0;for(a=0;a<u.length;a++){var p=u[a];if(!p.startsWith("|")||!p.endsWith("|"))break}r=r+a-1,i={type:"|",columns:u.slice(0,a).map(function(e){return e.split("|").slice(1,-1).map(function(e){return e.trim()})})}}else if(n.startsWith("---")||n.startsWith("***"))i={type:"hr",origin:n};else if(/^[>-]|\* /.test(n)||/^[0-9]. /.test(n)){var c=n.split(""),l=c[0],f=c.slice(1);if(Number.isNaN(+l))"-"===l&&(l="*"),i={type:l,inner:v([f.slice(1).join("")])[0],n:0,origin:n};else i={type:0,n:+l,inner:v([f.slice(2).join("")])[0],origin:n}}else if(n.startsWith("```")){var h=n.split(" ").slice(1),y=(u=e.slice(r+1)).findIndex(function(e){return"```"===e});r=r+y+1,i={type:"</>",params:h,code:u.slice(0,y).join("\n"),origin:n}}else i=""===n?{type:"br",origin:n}:{type:"p",text:n,origin:n};t.push(d.applyParses(i,n))}return t}function n(e){var t=v(e.split("\n").map(function(e){return e.trim()}));return d.applyAfterParsers(t)}function o(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||"|"===t.type)return[t].concat(o(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:o([t].concat(n).map(function(e){return e.inner}))}].concat(o(r.slice(n.length)))}function i(e){return o(n(e))}function p(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){r="<ol >"+p(t.block)+"</ol>"}else if("hr"===t.type)r="<hr />";else if("*"===t.type){r="<ul>"+p(t.block)+"</ul>"}else{if("|"===t.type){var i=t.columns,s=i[0],o=i[1]||[],u=i.slice(2),a=function(e){var t=o[e].trim();return{"::":"center"," :":"right",": ":"left"," ":"left"}[(":"===t[0]?":":" ")+(":"===t[t.length-1]?":":" ")]};return["<table>","<thead>"+("<tr>"+s.map(function(e,t){return'<th style="text-align: '+(a(t)||"")+'">'+e+"</th>"}).join("")+"</tr>")+"</thead>","<tbody>"+u.map(function(e){return"<tr>"+e.map(function(e,t){return'<td style="text-align: '+(a(t)||"")+'">'+e+"</td>"}).join("")+"</tr>"}).join("")+"</tbody>","</table>"].join("")}r="<blockquote>"+p(t.block)+"</blockquote>"}return e+d.applyRenders(t,r)},"")}e.fold=o,e.compile=i,e.astEval=p,e.render=function(e){return p(i(e))},e.getTokenFrom=v,e.parse=n,e.PluginCtx=t,e.ctx=d,e.use=r,e.createPlugin=function(e){return e},Object.defineProperty(e,"__esModule",{value:!0})});
{
"name": "down-parse",
"version": "2.3.1",
"version": "2.4.0",
"description": "markdown parser",

@@ -5,0 +5,0 @@ "main": "dist/main.js",

@@ -1,2 +0,12 @@

import { parse, Token, CodeToken, HeaderToken, BlockToken, ParaToken, BrToken, HrToken } from "./token";
import {
parse,
Token,
BrToken,
HrToken,
ParaToken,
CodeToken,
BlockToken,
TableToken,
HeaderToken
} from "./token";

@@ -9,2 +19,3 @@ // Base AST Node Type

export type CodeAST = CodeToken;
export type TableAST = TableToken;

@@ -22,3 +33,3 @@ // AST Type Generator

// AST Node Type
export type AST = ParaAST | BrAST | HrAST | BlockAST | HeaderAST | CodeAST;
export type AST = ParaAST | BrAST | HrAST | BlockAST | HeaderAST | CodeAST | TableAST;

@@ -39,3 +50,4 @@ /**

first.type === 'p' ||
first.type === 'hr'
first.type === 'hr' ||
first.type === '|'
) {

@@ -42,0 +54,0 @@ return [first, ...fold(rest)];

@@ -30,2 +30,38 @@ import { AST, compile } from "./compile";

defaultOutput = `<ul>${ inner }</ul>`;
} else if (ast.type === '|') {
const { columns } = ast;
const header = columns[0];
const align = columns[1] || [];
const items = columns.slice(2);
const getAlign = (n: number) => {
const k = align[n].trim();
const l = k[0] === ':' ? ':' : ' ';
const r = k[k.length - 1] === ':' ? ':' : ' ';
return ({
'::': 'center',
' :': 'right',
': ': 'left',
' ': 'left'
})[l + r];
}
const headerLines = `<tr>` + header.map(
(e, i) => `<th style="text-align: ${getAlign(i) || ''}">${e}</th>`
).join('') + `</tr>`;
const bodyLines = items.map(
e => `<tr>` + e.map((f, i) =>
`<td style="text-align: ${getAlign(i) || ''}">${ f }</td>`
).join('') + `</tr>`
).join('');
return [
`<table>`,
`<thead>${ headerLines }</thead>`,
`<tbody>${ bodyLines }</tbody>`,
`</table>`,
].join('');
} else {

@@ -32,0 +68,0 @@ const inner = astEval(ast.block);

@@ -20,3 +20,3 @@ import { ctx } from "./plugin";

text: string
}>;;
}>

@@ -26,4 +26,8 @@ export type CodeToken = GeneralToken<'</>', {

code: string
}>;
}>
export type TableToken = GeneralToken<'|', {
columns: string[][]
}>
export type HrToken = GeneralToken<'hr', {}>

@@ -33,3 +37,3 @@

export type Token = HeaderToken | BrToken | HrToken | ParaToken | CodeToken | BlockToken;
export type Token = HeaderToken | BrToken | HrToken | ParaToken | CodeToken | BlockToken | TableToken;

@@ -63,2 +67,21 @@ /**

} as HeaderToken;
} else if (lineOne.startsWith('|') && lineOne.endsWith('|')) {
const nexts = lines.slice(i);
let _ = 0;
for (_ = 0; _ < nexts.length; _ ++) {
const e = nexts[_];
if (!(e.startsWith('|') && e.endsWith('|'))) {
break;
}
}
i = i + _ - 1;
ret = {
type: '|',
columns: nexts.slice(0, _).map(e => e.split('|').slice(1, -1).map(e => e.trim())),
// origin: [lineOne, ...nexts.slice(0, _)].join('\n')
} as TableToken;
} else if (lineOne.startsWith('---') || lineOne.startsWith('***')) {

@@ -65,0 +88,0 @@ ret = {

@@ -1,38 +0,33 @@

const { render, use } = require('./dist/dev');
const { render, use, compile, parse } = require('./dist/dev');
use({
afterParser(tokens, setTokens) {
const newTokens = [];
for (let i = 0; i < tokens.length - 1; i ++) {
const now = tokens[i];
const next = tokens[i + 1];
// 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);
}
}
// if (now.type === 'br') {
// if (next.type !== 'br') {
// newTokens.push(now);
// }
// } else {
// newTokens.push(now);
// }
// }
console.log(newTokens)
setTokens(newTokens);
// console.log(newTokens)
// setTokens(newTokens);
}
});
const res = render(`
# I ❤️ Plugin
const text = require('fs').readFileSync(
// '/Users/ecznlai/Desktop/git-rally/eczn-blog/React 中使用 Highcharts.md',
'./test.md',
'utf-8'
);
pppp
p
`);
console.log('res', res);
const res = render(text);
// res.forEach(e => console.log(e));
console.log(res);
// => '<br /><h1>I ❤️ Plugin</h1><p>HELLO, 😊</p><br />'

@@ -1,18 +0,10 @@

``` js
console.log('!');
```
| Syntax | Description |
| ----------- | ----------- |
| Header | Title |
| Paragraph | Text |
| Oh Shit | Fuck The Table |
* 1
* 2
* 3
?
> 1
> > 2
> > > 3
> 1. 一行
> 2. 两行
> 3. 三行
1. a
2. b
3. c
123

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc