marktex.js
Advanced tools
Comparing version 0.1.2 to 0.1.3
"use strict"; | ||
Object.defineProperty(exports, "__esModule", {value: true}); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const parser_1 = require("./parser"); | ||
const renderer_1 = require("./renderer"); | ||
const source_1 = require("./source"); | ||
const rules_1 = require("./rules"); | ||
// noinspection JSUnusedGlobalSymbols | ||
@@ -12,5 +14,11 @@ const myriad = { | ||
Renderer(options) { | ||
return new renderer_1.Renderer(myriad.Parser(options), options === null || options === void 0 ? void 0 : options.renderOptions); | ||
return new renderer_1.Renderer(myriad.Parser(options), options === null || options === void 0 ? void 0 : options.rendererOptions); | ||
}, | ||
StringStream(str) { | ||
return new source_1.StringStream(str); | ||
}, | ||
newInlineRules: rules_1.newInlineRules, | ||
newBlockRules: rules_1.newBlockRules, | ||
newRules: rules_1.newRules, | ||
}; | ||
exports.myriad = myriad; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", {value: true}); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const token_1 = require("./token"); | ||
@@ -24,3 +24,4 @@ const rules_1 = require("./rules"); | ||
e.content += t.content; | ||
} else { | ||
} | ||
else { | ||
r.push(t); | ||
@@ -27,0 +28,0 @@ e = t; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", {value: true}); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const source_1 = require("./source"); | ||
@@ -43,2 +43,6 @@ const token_1 = require("./token"); | ||
} | ||
// noinspection JSUnusedGlobalSymbols | ||
renderString(s) { | ||
return this.render(new source_1.StringStream(s)); | ||
} | ||
renderElements(ctx, elements) { | ||
@@ -205,3 +209,2 @@ for (let el of elements) { | ||
} | ||
renderMathBlock(ctx, el) { | ||
@@ -213,3 +216,2 @@ let mathBlock = el; | ||
} | ||
renderLatexBlock(ctx, el) { | ||
@@ -220,3 +222,2 @@ let latexBlock = el; | ||
} | ||
renderEmphasis(ctx, el) { | ||
@@ -229,3 +230,2 @@ let emphasisEl = el; | ||
} | ||
renderInlineCode(ctx, el) { | ||
@@ -232,0 +232,0 @@ ctx.html += '<code>' + el.content + '</code>'; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", {value: true}); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const token_1 = require("./token"); | ||
@@ -80,15 +80,52 @@ const source_1 = require("./source"); | ||
} | ||
exports.NewLineRule = NewLineRule; | ||
class ParagraphRule { | ||
constructor() { | ||
constructor(skipLaTeXBlock) { | ||
this.name = "Paragraph"; | ||
this.description = "Standard Markdown Block Rule"; | ||
this.regex = /^(?:(?:[^$]|\$(?!\$))(?:\n|$)?)+/; | ||
this.skipLaTeXBlock = skipLaTeXBlock; | ||
} | ||
match(s, ctx) { | ||
let capturing = this.regex.exec(s.source); | ||
if (capturing === null) { | ||
let lastChar = 'a', i = 0; | ||
if (s.source[0] == '\n') { | ||
return undefined; | ||
} | ||
forward(s, capturing); | ||
return new token_1.Paragraph(ctx.parseInlineElements(new source_1.StringStream(capturing[0]))); | ||
if (this.skipLaTeXBlock) { | ||
for (; i < s.source.length; i++) { | ||
if (lastChar === s.source[i] && (lastChar === '$' || lastChar === '\n')) { | ||
i--; | ||
break; | ||
} | ||
if (lastChar === '\n' && s.source[i] === '\\') { | ||
if (i + 1 < s.source.length && | ||
(('a' <= s.source[i + 1] && s.source[i + 1] <= 'z') || ('A' <= s.source[i + 1] && s.source[i + 1] <= 'Z'))) { | ||
i--; | ||
break; | ||
} | ||
} else if (lastChar === '\\' && s.source[i] !== '\n') { | ||
lastChar = 'a'; | ||
} else { | ||
lastChar = s.source[i]; | ||
} | ||
} | ||
} else { | ||
for (; i < s.source.length; i++) { | ||
if (lastChar === s.source[i] && '$\n'.includes(lastChar)) { | ||
i--; | ||
break; | ||
} | ||
if (lastChar === '\\' && s.source[i] !== '\n') { | ||
lastChar = 'a'; | ||
} else { | ||
lastChar = s.source[i]; | ||
} | ||
} | ||
} | ||
if (!i) { | ||
return undefined; | ||
} | ||
let capturing = s.source.slice(0, i); | ||
s.forward(i); | ||
return new token_1.Paragraph(ctx.parseInlineElements(new source_1.StringStream(capturing))); | ||
} | ||
@@ -109,5 +146,7 @@ ; | ||
ordered = false; | ||
} else if ('0' <= s.source[0] && s.source[0] <= '9') { | ||
} | ||
else if ('0' <= s.source[0] && s.source[0] <= '9') { | ||
ordered = true; | ||
} else { | ||
} | ||
else { | ||
return undefined; | ||
@@ -144,3 +183,4 @@ } | ||
} | ||
} else { | ||
} | ||
else { | ||
element.blankSeparated = lastSeparated; | ||
@@ -277,5 +317,3 @@ lastSeparated = false; | ||
} | ||
exports.HTMLBlockRule = HTMLBlockRule; | ||
function _braceMatch(s, l, r) { | ||
@@ -287,3 +325,4 @@ if (s.source[0] == l) { | ||
c++; | ||
} else if (s.source[j] == r) { | ||
} | ||
else if (s.source[j] == r) { | ||
c--; | ||
@@ -303,3 +342,2 @@ if (c === 0) { | ||
} | ||
class InlineLatexCommandRule { | ||
@@ -310,3 +348,2 @@ constructor() { | ||
} | ||
match(s, _) { | ||
@@ -320,3 +357,2 @@ let capturing = InlineLatexCommandRule.cmdNameRegex.exec(s.source); | ||
} | ||
braceMatch(s) { | ||
@@ -340,6 +376,4 @@ let res = ''; | ||
} | ||
exports.InlineLatexCommandRule = InlineLatexCommandRule; | ||
InlineLatexCommandRule.cmdNameRegex = /^\\([a-zA-Z_]\w*)/; | ||
class LatexBlockRule { | ||
@@ -350,3 +384,2 @@ constructor() { | ||
} | ||
match(s, _) { | ||
@@ -360,3 +393,2 @@ let capturing = InlineLatexCommandRule.cmdNameRegex.exec(s.source); | ||
} | ||
braceMatch(s) { | ||
@@ -388,4 +420,4 @@ let res = ''; | ||
exports.LatexBlockRule = LatexBlockRule; | ||
LatexBlockRule.cmdNameRegex = /^\\([a-zA-Z_]\w*)/; | ||
class InlinePlainExceptSpecialMarksRule { | ||
@@ -397,3 +429,2 @@ constructor() { | ||
} | ||
match(s, _) { | ||
@@ -426,5 +457,3 @@ let capturing = this.regex.exec(s.source); | ||
} | ||
exports.InlinePlainRule = InlinePlainRule; | ||
class InlineMathRule { | ||
@@ -436,3 +465,2 @@ constructor() { | ||
} | ||
match(s, _) { | ||
@@ -448,5 +476,3 @@ let capturing = this.regex.exec(s.source); | ||
} | ||
exports.InlineMathRule = InlineMathRule; | ||
class MathBlockRule { | ||
@@ -458,3 +484,2 @@ constructor() { | ||
} | ||
match(s, _) { | ||
@@ -471,2 +496,3 @@ let capturing = this.regex.exec(s.source); | ||
exports.MathBlockRule = MathBlockRule; | ||
class LinkOrImageRule { | ||
@@ -479,3 +505,2 @@ constructor() { | ||
} | ||
match(s, ctx) { | ||
@@ -493,3 +518,4 @@ return this.matchInline(s, ctx) || this.matchRef(s, ctx); | ||
return new token_1.ImageLink(capturing[2], capturing[3], true, capturing[4]); | ||
} else { | ||
} | ||
else { | ||
return new token_1.Link(capturing[2], capturing[3], true, capturing[4]); | ||
@@ -506,3 +532,4 @@ } | ||
return new token_1.ImageLink(capturing[2], capturing[3], false); | ||
} else { | ||
} | ||
else { | ||
return new token_1.Link(capturing[2], capturing[3], false); | ||
@@ -573,9 +600,9 @@ } | ||
} | ||
exports.GFMFencedCodeBlockRule = GFMFencedCodeBlockRule; | ||
GFMFencedCodeBlockRule.backtickRegex = /^(`{3,}) *([^`\s]+)?[^`\n]*(?:\n|$)([\s\S]*?)(?:\1`*|$)/; | ||
GFMFencedCodeBlockRule.tildeRegex = /^(~{3,}) *([^~\s]+)?.*(?:\n|$)([\s\S]*?)(?:\1~*|$)/; | ||
const inlineRules = newInlineRules(); | ||
exports.inlineRules = inlineRules; | ||
const blockRules = newBlockRules(); | ||
exports.blockRules = blockRules; | ||
exports.inlineRules = newInlineRules(); | ||
exports.blockRules = newBlockRules(); | ||
// noinspection JSUnusedGlobalSymbols | ||
@@ -597,3 +624,3 @@ function newBlockRules(opts) { | ||
let rules2 = [ | ||
new ParagraphRule(), | ||
new ParagraphRule((opts === null || opts === void 0 ? void 0 : opts.enableLaTeX) || false), | ||
]; | ||
@@ -609,5 +636,3 @@ // default enable | ||
} | ||
exports.newBlockRules = newBlockRules; | ||
// noinspection JSUnusedGlobalSymbols | ||
@@ -633,1 +658,10 @@ function newInlineRules(opts) { | ||
exports.newInlineRules = newInlineRules; | ||
function newRules(opts) { | ||
return { | ||
inlineRules: newInlineRules(opts), | ||
blockRules: newBlockRules(opts), | ||
}; | ||
} | ||
exports.newRules = newRules; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", {value: true}); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
class StringStream { | ||
@@ -4,0 +4,0 @@ constructor(source) { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", {value: true}); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const source_1 = require("./source"); | ||
@@ -13,3 +13,3 @@ const parser_1 = require("./parser"); | ||
function itWillMatchElement(rule, ctx = createContext()) { | ||
return ({title, text, matchedLength, expectedElement}) => { | ||
return ({ title, text, matchedLength, expectedElement }) => { | ||
it(title ? title : ('will match ' + text), () => { | ||
@@ -24,3 +24,3 @@ let stream = new source_1.StringStream(text); | ||
function itWillNotMatchElement(rule, ctx = createContext()) { | ||
return ({title, text}) => { | ||
return ({ title, text }) => { | ||
it(title ? title : ('will not match ' + text), () => { | ||
@@ -33,3 +33,3 @@ exports.expect(rule.match(new source_1.StringStream(text), ctx)).to.be.equal(undefined); | ||
function benchText(suite, rule, ctx = createContext()) { | ||
return ({title, text}) => { | ||
return ({ title, text }) => { | ||
suite.add(title ? title : ('test match ' + text), () => { | ||
@@ -46,4 +46,4 @@ rule.match(new source_1.StringStream(text), ctx); | ||
console.log(String(event.target)); | ||
}).run({'async': true}); | ||
}).run({ 'async': true }); | ||
} | ||
exports.onRun = onRun; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", {value: true}); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const source_1 = require("./source"); | ||
@@ -53,5 +53,3 @@ var BraceType; | ||
} | ||
exports.expectBraceType = expectBraceType; | ||
function expectTheOnlyBrace(vars, tracer) { | ||
@@ -68,5 +66,3 @@ if (vars.length < 1) { | ||
} | ||
exports.expectTheOnlyBrace = expectTheOnlyBrace; | ||
function releaseVars(vars, n) { | ||
@@ -91,3 +87,2 @@ let res = ''; | ||
} | ||
let replaceRegex = [ | ||
@@ -178,3 +173,3 @@ /(?<!\\)#1/g, /(?<!\\)#2/g, /(?<!\\)#3/g, /(?<!\\)#4/g, | ||
} | ||
return '<br/>' + tex(ctx, new source_1.StringStream(releaseVars(vars, 0))); | ||
return /*'<br/>' + */ tex(ctx, new source_1.StringStream(releaseVars(vars, 0))); | ||
}, | ||
@@ -238,6 +233,7 @@ url(ctx, vars, tex) { | ||
c++; | ||
} else if (s.source[j] == r) { | ||
} | ||
else if (s.source[j] == r) { | ||
c--; | ||
if (c === 0) { | ||
res.push({braceType: t, text: s.source.slice(1, j)}); | ||
res.push({ braceType: t, text: s.source.slice(1, j) }); | ||
s.forward(j + 1); | ||
@@ -284,3 +280,4 @@ return; | ||
markdownText += cmd(ctx, vars, this.tex); | ||
} else { | ||
} | ||
else { | ||
markdownText += s.source.slice(0, capturing.index + capturing[0].length); | ||
@@ -287,0 +284,0 @@ s.forward(capturing.index + capturing[0].length); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", {value: true}); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var TokenType; | ||
@@ -24,5 +24,3 @@ (function (TokenType) { | ||
// noinspection JSUnusedGlobalSymbols | ||
const StdBlockTokenL = TokenType.Paragraph, StdBlockTokenR = TokenType.HeaderBlock + 1, | ||
StdInlineTokenL = TokenType.InlinePlain, StdInlineTokenR = TokenType.InlineCode + 1, | ||
StdBlockTokenCount = StdBlockTokenR - StdBlockTokenL, StdInlineTokenCount = StdInlineTokenR - StdInlineTokenL; | ||
const StdBlockTokenL = TokenType.Paragraph, StdBlockTokenR = TokenType.HeaderBlock + 1, StdInlineTokenL = TokenType.InlinePlain, StdInlineTokenR = TokenType.InlineCode + 1, StdBlockTokenCount = StdBlockTokenR - StdBlockTokenL, StdInlineTokenCount = StdInlineTokenR - StdInlineTokenL; | ||
exports.StdBlockTokenCount = StdBlockTokenCount; | ||
@@ -215,3 +213,4 @@ exports.StdInlineTokenCount = StdInlineTokenCount; | ||
return ListBlock.lookAheadOrderedListNumber(s); | ||
} else { | ||
} | ||
else { | ||
return ListBlock.lookAheadUnorderedListMarker(s); | ||
@@ -223,3 +222,4 @@ } | ||
return '0' <= s.source[0] && s.source[0] <= '9'; | ||
} else if ('*+-'.includes(s.source[0])) { | ||
} | ||
else if ('*+-'.includes(s.source[0])) { | ||
let j = 0; | ||
@@ -229,3 +229,4 @@ for (let i = 0; s.source[i] && s.source[i] != '\n'; i++) { | ||
j++; | ||
} else if (!'\t\r\v\f '.includes(s.source[i])) { | ||
} | ||
else if (!'\t\r\v\f '.includes(s.source[i])) { | ||
j = 0; | ||
@@ -240,3 +241,3 @@ break; | ||
static lookAheadOrderedListNumber(s) { | ||
for (let i = 0; ; i++) { | ||
for (let i = 0;; i++) { | ||
if ('0' <= s.source[i] && s.source[i] <= '9') { | ||
@@ -411,5 +412,3 @@ continue; | ||
} | ||
exports.Emphasis = Emphasis; | ||
class InlineCode { | ||
@@ -421,5 +420,3 @@ constructor(content) { | ||
} | ||
exports.InlineCode = InlineCode; | ||
class LateXBlock { | ||
@@ -431,5 +428,3 @@ constructor(content) { | ||
} | ||
exports.LateXBlock = LateXBlock; | ||
class MathBlock { | ||
@@ -442,3 +437,2 @@ constructor(content, inline) { | ||
} | ||
exports.MathBlock = MathBlock; |
{ | ||
"name": "marktex.js", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "markdown parser", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -12,1 +12,7 @@ import {myriad} from './index'; | ||
describe('renderer', () => { | ||
it('can render with default option', () => { | ||
console.log(myriad.Renderer().renderString('## hello marktex.js')) | ||
}) | ||
}); | ||
import {Parser, ParserOptions} from "./parser"; | ||
import {Renderer, RenderOptions} from "./renderer"; | ||
import {StringStream} from "./source"; | ||
import {newBlockRules, newInlineRules, newRules} from "./rules"; | ||
@@ -7,3 +9,3 @@ | ||
parserOptions?: ParserOptions; | ||
renderOptions?: RenderOptions; | ||
rendererOptions?: RenderOptions; | ||
} | ||
@@ -17,6 +19,11 @@ | ||
}, | ||
Renderer(options?: Options): Renderer { | ||
return new Renderer(myriad.Parser(options), options?.renderOptions); | ||
return new Renderer(myriad.Parser(options), options?.rendererOptions); | ||
}, | ||
StringStream(str: string): StringStream { | ||
return new StringStream(str); | ||
}, | ||
newInlineRules, | ||
newBlockRules, | ||
newRules, | ||
}; | ||
@@ -23,0 +30,0 @@ |
import Benchmark = require("benchmark"); | ||
import {createContext} from "./test_util"; | ||
import {cn_book_md, math_md} from "./data.test"; | ||
import {StringStream} from "./source"; | ||
import {Parser} from "./parser"; | ||
import {newRules} from "./rules"; | ||
let suite = new Benchmark.Suite; | ||
let ctx = createContext(); | ||
let ctx = new Parser(newRules({enableLaTeX: true})); | ||
@@ -10,0 +11,0 @@ |
import {StringStream} from "./source"; | ||
import {cn_book_md, latex_md, math_md} from "./data.test"; | ||
import {Parser} from "./parser"; | ||
import {newInlineRules} from "./rules"; | ||
import {newRules} from "./rules"; | ||
@@ -9,5 +9,3 @@ | ||
'parser', () => { | ||
let ctx = new Parser({ | ||
inlineRules: newInlineRules({enableLaTeX: true}), | ||
}); | ||
let ctx = new Parser(newRules({enableLaTeX: true})); | ||
it('process math.md', () => { | ||
@@ -14,0 +12,0 @@ console.log(JSON.stringify(ctx.parseBlockElements(new StringStream(math_md)), undefined, 2)); |
@@ -5,3 +5,3 @@ import {Renderer} from "./renderer"; | ||
import {Parser} from "./parser"; | ||
import {newInlineRules} from "./rules"; | ||
import {newRules} from "./rules"; | ||
@@ -15,5 +15,3 @@ | ||
it('should render math.md', () => { | ||
console.log(new Renderer(new Parser({ | ||
inlineRules: newInlineRules({enableLaTeX: true}), | ||
}), {enableLaTeX: true}).render( | ||
console.log(new Renderer(new Parser(newRules({enableLaTeX: true})), {enableLaTeX: true}).render( | ||
new StringStream(math_md), | ||
@@ -23,5 +21,3 @@ )); | ||
it('should render latex.md', () => { | ||
console.log(new Renderer(new Parser({ | ||
inlineRules: newInlineRules({enableLaTeX: true}), | ||
}), {enableLaTeX: true}).render( | ||
console.log(new Renderer(new Parser(newRules({enableLaTeX: true})), {enableLaTeX: true}).render( | ||
new StringStream(latex_md), | ||
@@ -28,0 +24,0 @@ )); |
@@ -95,2 +95,7 @@ import {Parser} from "./parser"; | ||
// noinspection JSUnusedGlobalSymbols | ||
public renderString(s: string): string { | ||
return this.render(new StringStream(s)); | ||
} | ||
public renderElements(ctx: RenderContext, elements: Token[]) { | ||
@@ -102,3 +107,3 @@ for (let el of elements) { | ||
public createLinkMap(ctx: RenderContext) { | ||
protected createLinkMap(ctx: RenderContext) { | ||
for (let el of ctx.tokens) { | ||
@@ -118,3 +123,3 @@ if (el.token_type === TokenType.LinkDefinition) { | ||
public handleElement(ctx: RenderContext, el: BlockElement) { | ||
protected handleElement(ctx: RenderContext, el: BlockElement) { | ||
switch (el.token_type) { | ||
@@ -121,0 +126,0 @@ case TokenType.Paragraph: |
@@ -29,2 +29,3 @@ import { | ||
ListElement, | ||
MathBlock, | ||
NewLine, | ||
@@ -114,9 +115,9 @@ Paragraph, | ||
match({text: "ggg\n\nggg", matchedLength: "ggg\n\nggg".length, expectedElement: new InlinePlain("ggg\n\nggg")}); | ||
match({text: " `", matchedLength: 4, expectedElement: new InlinePlain(" `")}); | ||
match({text: "ggg<", matchedLength: 4, expectedElement: new InlinePlain("ggg<")}); | ||
match({text: "ggg[", matchedLength: 4, expectedElement: new InlinePlain("ggg[")}); | ||
match({text: "ggg*", matchedLength: 4, expectedElement: new InlinePlain("ggg*")}); | ||
match({text: "ggg_", matchedLength: 4, expectedElement: new InlinePlain("ggg_")}); | ||
match({text: "ggg`", matchedLength: 4, expectedElement: new InlinePlain("ggg`")}); | ||
match({text: "铜`", matchedLength: 2, expectedElement: new InlinePlain("铜`")}); | ||
match({text: " ", matchedLength: 3, expectedElement: new InlinePlain(" ")}); | ||
match({text: "ggg<", matchedLength: 3, expectedElement: new InlinePlain("ggg")}); | ||
match({text: "ggg[", matchedLength: 3, expectedElement: new InlinePlain("ggg")}); | ||
match({text: "ggg*", matchedLength: 3, expectedElement: new InlinePlain("ggg")}); | ||
match({text: "ggg_", matchedLength: 3, expectedElement: new InlinePlain("ggg")}); | ||
match({text: "ggg`", matchedLength: 3, expectedElement: new InlinePlain("ggg")}); | ||
match({text: "铜", matchedLength: 1, expectedElement: new InlinePlain("铜")}); | ||
notMatch({text: ""}); | ||
@@ -357,3 +358,3 @@ match({text: "<", matchedLength: 1, expectedElement: new InlinePlain("<")}); | ||
describe("ParagraphRule", () => { | ||
let rule: ParagraphRule = new ParagraphRule(); | ||
let rule: ParagraphRule = new ParagraphRule(true); | ||
let match: elementMatcher = itWillMatchElement(rule); | ||
@@ -383,8 +384,44 @@ let notMatch: textAcceptor = itWillNotMatchElement(rule); | ||
text: "a\n\nqwq", | ||
matchedLength: "a\n".length, | ||
matchedLength: "a".length, | ||
expectedElement: new Paragraph( | ||
[new InlinePlain("a\n")], | ||
[new InlinePlain("a")], | ||
), | ||
}); | ||
match({ | ||
text: "a\\inlineLatexRule\n\nqwq", | ||
matchedLength: "a\\inlineLatexRule".length, | ||
expectedElement: new Paragraph( | ||
[new InlinePlain("a\\inlineLatexRule")], | ||
), | ||
}); | ||
match({ | ||
text: "a\\inlineLatexRule{}{}\n\nqwq", | ||
matchedLength: "a\\inlineLatexRule{}{}".length, | ||
expectedElement: new Paragraph( | ||
[new InlinePlain("a\\inlineLatexRule{}{}")], | ||
), | ||
}); | ||
match({ | ||
text: "a\n\\blockLatexRule\n\nqwq", | ||
matchedLength: "a".length, | ||
expectedElement: new Paragraph( | ||
[new InlinePlain("a")], | ||
), | ||
}); | ||
match({ | ||
text: '$A[1...n]=[A[1]...A[n]](n>=1)$总是表示一个数组,$len[A]=n$总是表示一个数组的长度.\n' + | ||
'\\subsubsection{数组切片}\n', | ||
matchedLength: '$A[1...n]=[A[1]...A[n]](n>=1)$总是表示一个数组,$len[A]=n$总是表示一个数组的长度.'.length, | ||
expectedElement: new Paragraph( | ||
[ | ||
new MathBlock("A[1...n]=[A[1]...A[n]](n>=1)", true), | ||
new InlinePlain("总是表示一个数组,"), | ||
new MathBlock("len[A]=n", true), | ||
new InlinePlain("总是表示一个数组的长度."), | ||
], | ||
), | ||
}); | ||
match({ | ||
text: "a\nqwq", | ||
@@ -705,4 +742,4 @@ matchedLength: "a\nqwq".length, | ||
expectedElement: new Quotes([ | ||
new Paragraph([new InlinePlain("a\nqwq\n")]), | ||
new NewLine('\n'), | ||
new Paragraph([new InlinePlain("a\nqwq")]), | ||
new NewLine('\n\n'), | ||
new Paragraph([new InlinePlain("a\n")]) | ||
@@ -709,0 +746,0 @@ ]), |
126
src/rules.ts
@@ -62,3 +62,3 @@ import { | ||
interface RuleContext { | ||
export interface RuleContext { | ||
@@ -74,3 +74,3 @@ parseBlockElement(source: StringStream): BlockElement | ||
interface Rule { | ||
export interface Rule { | ||
readonly name?: string | ||
@@ -131,3 +131,3 @@ readonly description?: string | ||
class NewLineRule implements Rule { | ||
export class NewLineRule implements Rule { | ||
readonly name: string = "NewLine"; | ||
@@ -149,16 +149,57 @@ readonly description: string = "Standard Markdown Block Rule"; | ||
class ParagraphRule implements Rule { | ||
export class ParagraphRule implements Rule { | ||
readonly name: string = "Paragraph"; | ||
readonly description: string = "Standard Markdown Block Rule"; | ||
public readonly regex: RegExp = /^(?:(?:[^$]|\$(?!\$))(?:\n|$)?)+/; | ||
// public readonly regex: RegExp = /^(?:(?:[^$]|\$(?!\$))(?:\n|$)?)+/; | ||
protected readonly skipLaTeXBlock: boolean; | ||
constructor(skipLaTeXBlock: boolean) { | ||
this.skipLaTeXBlock = skipLaTeXBlock; | ||
} | ||
match(s: StringStream, ctx: RuleContext): MaybeToken { | ||
let capturing = this.regex.exec(s.source); | ||
if (capturing === null) { | ||
let lastChar: string = 'a', i = 0; | ||
if (s.source[0] == '\n') { | ||
return undefined; | ||
} | ||
forward(s, capturing); | ||
return new Paragraph(ctx.parseInlineElements(new StringStream(capturing[0]))); | ||
if (this.skipLaTeXBlock) { | ||
for (; i < s.source.length; i++) { | ||
if (lastChar === s.source[i] && (lastChar === '$' || lastChar === '\n')) { | ||
i--; | ||
break; | ||
} | ||
if (lastChar === '\n' && s.source[i] === '\\') { | ||
if (i + 1 < s.source.length && | ||
(('a' <= s.source[i + 1] && s.source[i + 1] <= 'z') || ('A' <= s.source[i + 1] && s.source[i + 1] <= 'Z')) | ||
) { | ||
i--; | ||
break; | ||
} | ||
} else if (lastChar === '\\' && s.source[i] !== '\n') { | ||
lastChar = 'a'; | ||
} else { | ||
lastChar = s.source[i]; | ||
} | ||
} | ||
} else { | ||
for (; i < s.source.length; i++) { | ||
if (lastChar === s.source[i] && '$\n'.includes(lastChar)) { | ||
i--; | ||
break; | ||
} | ||
if (lastChar === '\\' && s.source[i] !== '\n') { | ||
lastChar = 'a'; | ||
} else { | ||
lastChar = s.source[i]; | ||
} | ||
} | ||
} | ||
if (!i) { | ||
return undefined; | ||
} | ||
let capturing = s.source.slice(0, i); | ||
s.forward(i); | ||
return new Paragraph(ctx.parseInlineElements(new StringStream(capturing))); | ||
}; | ||
@@ -171,3 +212,3 @@ } | ||
/* 1. */ | ||
class ListBlockRule implements Rule { | ||
export class ListBlockRule implements Rule { | ||
readonly name: string = "ListBlock"; | ||
@@ -233,3 +274,3 @@ readonly description: string = "Standard Markdown Block Rule"; | ||
class QuotesRule implements Rule { | ||
export class QuotesRule implements Rule { | ||
readonly name: string = "Quotes"; | ||
@@ -253,3 +294,3 @@ readonly description: string = "Standard Markdown Block Rule"; | ||
class HorizontalRule implements Rule { | ||
export class HorizontalRule implements Rule { | ||
readonly name: string = "Horizontal"; | ||
@@ -272,3 +313,3 @@ readonly description: string = "Standard Markdown Block Rule"; | ||
class CodeBlockRule implements Rule { | ||
export class CodeBlockRule implements Rule { | ||
readonly name: string = "CodeBlock"; | ||
@@ -290,3 +331,3 @@ readonly description: string = "Standard Markdown Block Rule"; | ||
class HeaderBlockRule implements Rule { | ||
export class HeaderBlockRule implements Rule { | ||
readonly name: string = "HeaderBlock"; | ||
@@ -321,3 +362,3 @@ readonly description: string = "Standard Markdown Block Rule"; | ||
class LinkDefinitionRule implements Rule { | ||
export class LinkDefinitionRule implements Rule { | ||
readonly name: string = "LinkDefinition"; | ||
@@ -337,3 +378,3 @@ readonly description: string = "Standard Markdown Block Rule"; | ||
class HTMLBlockRule implements Rule { | ||
export class HTMLBlockRule implements Rule { | ||
readonly name: string = "HTMLBlock"; | ||
@@ -388,3 +429,3 @@ readonly description: string = "Standard Markdown Block Rule"; | ||
class InlineLatexCommandRule implements Rule { | ||
export class InlineLatexCommandRule implements Rule { | ||
readonly name: string = "InlineLatexCommand"; | ||
@@ -424,3 +465,3 @@ readonly description: string = "Latex Inline Rule"; | ||
class LatexBlockRule implements Rule { | ||
export class LatexBlockRule implements Rule { | ||
readonly name: string = "LatexBlock"; | ||
@@ -467,3 +508,3 @@ readonly description: string = "Latex Inline Rule"; | ||
class InlinePlainExceptSpecialMarksRule implements Rule { | ||
export class InlinePlainExceptSpecialMarksRule implements Rule { | ||
readonly name: string = "InlinePlainExceptSpecialMarks"; | ||
@@ -485,3 +526,3 @@ readonly description: string = "Standard Markdown Inline Rule"; | ||
class InlinePlainRule implements Rule { | ||
export class InlinePlainRule implements Rule { | ||
readonly name: string = "InlinePlain"; | ||
@@ -503,3 +544,3 @@ readonly description: string = "Standard Markdown Inline Rule"; | ||
class InlineMathRule implements Rule { | ||
export class InlineMathRule implements Rule { | ||
readonly name: string = "InlineMath"; | ||
@@ -521,3 +562,3 @@ readonly description: string = "Markdown Inline Rule"; | ||
class MathBlockRule implements Rule { | ||
export class MathBlockRule implements Rule { | ||
readonly name: string = "MathBlock"; | ||
@@ -540,3 +581,3 @@ readonly description: string = "Markdown Block Rule"; | ||
class LinkOrImageRule implements Rule { | ||
export class LinkOrImageRule implements Rule { | ||
readonly name: string = "Link"; | ||
@@ -581,3 +622,3 @@ readonly description: string = "Standard Markdown Inline Rule"; | ||
class EmphasisRule implements Rule { | ||
export class EmphasisRule implements Rule { | ||
readonly name: string = "Emphasis"; | ||
@@ -609,3 +650,3 @@ readonly description: string = "Standard Markdown Inline Rule"; | ||
class InlineCodeRule implements Rule { | ||
export class InlineCodeRule implements Rule { | ||
readonly name: string = "InlineCode"; | ||
@@ -628,3 +669,3 @@ readonly description: string = "Standard Markdown Inline Rule"; | ||
class GFMFencedCodeBlockRule implements Rule { | ||
export class GFMFencedCodeBlockRule implements Rule { | ||
readonly name: string = "GFMCodeBlock"; | ||
@@ -650,8 +691,8 @@ readonly description: string = "GFM Markdown Block Rule"; | ||
const inlineRules: Rule[] = newInlineRules(); | ||
export const inlineRules: Rule[] = newInlineRules(); | ||
export const blockRules: Rule[] = newBlockRules(); | ||
const blockRules: Rule[] = newBlockRules(); | ||
export interface CreateBlockRuleOptions { | ||
enableHtml?: boolean; | ||
enableLaTeX?: boolean; | ||
enableGFMRules?: boolean; | ||
@@ -680,3 +721,3 @@ validTags?: HTMLTagsRegexps; | ||
let rules2: Rule[] = [ | ||
new ParagraphRule(), | ||
new ParagraphRule(opts?.enableLaTeX || false), | ||
]; | ||
@@ -727,19 +768,10 @@ | ||
export function newRules(opts?: CreateRuleOptions) { | ||
return { | ||
inlineRules: newInlineRules(opts), | ||
blockRules: newBlockRules(opts), | ||
} | ||
} | ||
export {Rule, RuleContext, validTags, inlineRules, blockRules}; | ||
export { | ||
InlinePlainExceptSpecialMarksRule, LinkOrImageRule, InlineLatexCommandRule, | ||
InlineMathRule, EmphasisRule, InlineCodeRule, InlinePlainRule, | ||
}; | ||
export { | ||
CodeBlockRule, | ||
GFMFencedCodeBlockRule, | ||
ParagraphRule, | ||
LinkDefinitionRule, | ||
QuotesRule, | ||
HTMLBlockRule, | ||
ListBlockRule, | ||
HorizontalRule, | ||
HeaderBlockRule, | ||
}; | ||
export {validTags}; |
@@ -40,3 +40,3 @@ import {StringStream} from "./source"; | ||
export type commandFunc = ( | ||
ctx: TexContext, vars: TexCmdVar[], tex: (ctx: TexContext, s: StringStream) => void) => string; | ||
ctx: TexContext, vars: TexCmdVar[], tex: (ctx: TexContext, s: StringStream) => string) => string; | ||
@@ -167,3 +167,3 @@ export interface TexContext { | ||
let optionVars = vars.slice(1, vars.length - 1), textTemplate = vars[vars.length - 1].text; | ||
ctx.texCommandDefs[cmdName] = function (ctx: TexContext, args: TexCmdVar[], tex: (ctx: TexContext, s: StringStream) => void): any { | ||
ctx.texCommandDefs[cmdName] = function (ctx: TexContext, args: TexCmdVar[], tex: (ctx: TexContext, s: StringStream) => string): any { | ||
let tracer = traceInvalidCommand(cmdName, ctx); | ||
@@ -200,3 +200,3 @@ | ||
}, | ||
par(ctx: TexContext, vars: TexCmdVar[], tex: (ctx: TexContext, s: StringStream) => void): string { | ||
par(ctx: TexContext, vars: TexCmdVar[], tex: (ctx: TexContext, s: StringStream) => string): string { | ||
if (ctx.underMathEnv) { | ||
@@ -208,9 +208,9 @@ return '\\par'; | ||
}, | ||
indent(ctx: TexContext, vars: TexCmdVar[], tex: (ctx: TexContext, s: StringStream) => void): string { | ||
indent(ctx: TexContext, vars: TexCmdVar[], tex: (ctx: TexContext, s: StringStream) => string): string { | ||
if (ctx.underMathEnv) { | ||
return '\\indent'; | ||
} | ||
return '<br/>' + tex(ctx, new StringStream(releaseVars(vars, 0))); | ||
return /*'<br/>' + */ tex(ctx, new StringStream(releaseVars(vars, 0))); | ||
}, | ||
url(ctx: TexContext, vars: TexCmdVar[], tex: (ctx: TexContext, s: StringStream) => void): string { | ||
url(ctx: TexContext, vars: TexCmdVar[], tex: (ctx: TexContext, s: StringStream) => string): string { | ||
if (ctx.underMathEnv) { | ||
@@ -225,3 +225,3 @@ return ''; | ||
}, | ||
section(ctx: TexContext, vars: TexCmdVar[], tex: (ctx: TexContext, s: StringStream) => void): string { | ||
section(ctx: TexContext, vars: TexCmdVar[], tex: (ctx: TexContext, s: StringStream) => string): string { | ||
if (ctx.underMathEnv) { | ||
@@ -236,3 +236,3 @@ return ''; | ||
}, | ||
subsection(ctx: TexContext, vars: TexCmdVar[], tex: (ctx: TexContext, s: StringStream) => void): string { | ||
subsection(ctx: TexContext, vars: TexCmdVar[], tex: (ctx: TexContext, s: StringStream) => string): string { | ||
if (ctx.underMathEnv) { | ||
@@ -247,3 +247,3 @@ return ''; | ||
}, | ||
subsubsection(ctx: TexContext, vars: TexCmdVar[], tex: (ctx: TexContext, s: StringStream) => void): string { | ||
subsubsection(ctx: TexContext, vars: TexCmdVar[], tex: (ctx: TexContext, s: StringStream) => string): string { | ||
if (ctx.underMathEnv) { | ||
@@ -258,3 +258,3 @@ return ''; | ||
}, | ||
subsubsubsection(ctx: TexContext, vars: TexCmdVar[], tex: (ctx: TexContext, s: StringStream) => void): string { | ||
subsubsubsection(ctx: TexContext, vars: TexCmdVar[], tex: (ctx: TexContext, s: StringStream) => string): string { | ||
if (ctx.underMathEnv) { | ||
@@ -261,0 +261,0 @@ return ''; |
@@ -15,3 +15,3 @@ { | ||
"src/**/*.test.ts", | ||
"src/**/*.bench.ts", | ||
// "src/**/*.bench.ts", | ||
"dist" | ||
@@ -18,0 +18,0 @@ ], |
Sorry, the diff of this file is too big to display
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
437359
32
6784