marktex.js
Advanced tools
Comparing version 0.3.7 to 0.4.1-2
import { Parser } from "./parser/parser"; | ||
import { HighlightFunc, Renderer, RenderMiddleware } from "./renderer/renderer"; | ||
import { HighlightFunc, Renderer } from "./renderer/renderer"; | ||
import { StringStream } from "./lib/stream"; | ||
import { newBlockRules, newInlineRules, newRules } from "./rules"; | ||
import { HTMLBlockOptions } from "./rules/std"; | ||
export interface MarkTeXParserOptions { | ||
import { RenderDriver, RenderMiddleware } from "./driver/driver"; | ||
interface MarkTeXBasicEnableOptions { | ||
enableLaTeX?: boolean; | ||
enableGFMRules?: boolean; | ||
enableHtml?: boolean; | ||
} | ||
export interface MarkTeXParserOptions extends MarkTeXBasicEnableOptions { | ||
HTMLBlockOptions?: HTMLBlockOptions; | ||
} | ||
export interface MarkTeXRendererOptions extends MarkTeXParserOptions { | ||
parser?: Parser; | ||
export interface MarkTeXRendererOptions extends MarkTeXBasicEnableOptions { | ||
renderStyle?: 'default' | 'gfm' | 'latex'; | ||
highlight?: HighlightFunc; | ||
@@ -18,2 +21,6 @@ wrapCodeClassTag?: (language: string) => string; | ||
} | ||
export interface MarkTeXRenderDriverOptions extends MarkTeXParserOptions, MarkTeXRendererOptions { | ||
parser?: Parser; | ||
renderer?: Renderer; | ||
} | ||
declare const myriad: { | ||
@@ -23,2 +30,3 @@ author: string; | ||
newRenderer(options?: MarkTeXRendererOptions | undefined): Renderer; | ||
newRenderDriver(options?: MarkTeXRenderDriverOptions | undefined): RenderDriver; | ||
newStringStream(str: string): StringStream; | ||
@@ -30,5 +38,6 @@ newInlineRules: typeof newInlineRules; | ||
Renderer: typeof Renderer; | ||
RenderDriver: typeof RenderDriver; | ||
StringStream: typeof StringStream; | ||
}; | ||
export default myriad; | ||
export { myriad, Parser, Renderer, StringStream }; | ||
export { myriad, Parser, Renderer, RenderDriver, StringStream }; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.StringStream = exports.RenderDriver = exports.Renderer = exports.Parser = exports.myriad = void 0; | ||
const parser_1 = require("./parser/parser"); | ||
exports.Parser = parser_1.Parser; | ||
Object.defineProperty(exports, "Parser", { enumerable: true, get: function () { return parser_1.Parser; } }); | ||
const renderer_1 = require("./renderer/renderer"); | ||
exports.Renderer = renderer_1.Renderer; | ||
Object.defineProperty(exports, "Renderer", { enumerable: true, get: function () { return renderer_1.Renderer; } }); | ||
const stream_1 = require("./lib/stream"); | ||
exports.StringStream = stream_1.StringStream; | ||
Object.defineProperty(exports, "StringStream", { enumerable: true, get: function () { return stream_1.StringStream; } }); | ||
const rules_1 = require("./rules"); | ||
const driver_1 = require("./driver/driver"); | ||
Object.defineProperty(exports, "RenderDriver", { enumerable: true, get: function () { return driver_1.RenderDriver; } }); | ||
// | ||
@@ -22,4 +25,7 @@ // originStack?: RenderMiddleware[], | ||
newRenderer(options) { | ||
return new renderer_1.Renderer((options === null || options === void 0 ? void 0 : options.parser) || myriad.newParser(options), options); | ||
return (options === null || options === void 0 ? void 0 : options.renderStyle) === 'latex' ? new renderer_1.LaTeXRenderer(options) : new renderer_1.Renderer(options); | ||
}, | ||
newRenderDriver(options) { | ||
return new driver_1.RenderDriver((options === null || options === void 0 ? void 0 : options.parser) || myriad.newParser(options), (options === null || options === void 0 ? void 0 : options.renderer) || myriad.newRenderer(options), options); | ||
}, | ||
newStringStream(str) { | ||
@@ -29,3 +35,3 @@ return new stream_1.StringStream(str); | ||
newInlineRules: rules_1.newInlineRules, newBlockRules: rules_1.newBlockRules, newRules: rules_1.newRules, | ||
Parser: parser_1.Parser, Renderer: renderer_1.Renderer, StringStream: stream_1.StringStream, | ||
Parser: parser_1.Parser, Renderer: renderer_1.Renderer, RenderDriver: driver_1.RenderDriver, StringStream: stream_1.StringStream, | ||
}; | ||
@@ -32,0 +38,0 @@ exports.myriad = myriad; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.unescapeBackSlash = exports.escapeHTML = void 0; | ||
function escapeHTML(s) { | ||
@@ -4,0 +5,0 @@ return s.replace(/&/g, '&') |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.maybeCompose = exports.Id = exports.compose = void 0; | ||
exports.compose = (g, f) => (i) => g(f(i)); | ||
@@ -4,0 +5,0 @@ function Id(i) { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.forwardRegexp = exports.StringStream = void 0; | ||
class StringStream { | ||
@@ -4,0 +5,0 @@ constructor(source) { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.TrieNode = void 0; | ||
class TrieNode { | ||
@@ -4,0 +5,0 @@ constructor(ch, dep = 0) { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Parser = void 0; | ||
const token_1 = require("../token/token"); | ||
@@ -4,0 +5,0 @@ const rules_1 = require("../rules"); |
@@ -19,3 +19,3 @@ import { StringStream } from ".."; | ||
export declare type commandFunc = (ctx: TexContext, vars: TexCmdVar[], tex: (ctx: TexContext, s: StringStream) => string) => string; | ||
export interface TexContext { | ||
export declare type TexContext<TexExtends = any> = { | ||
readonly texCommands: { | ||
@@ -28,3 +28,3 @@ [commandName: string]: commandFunc | undefined; | ||
underMathEnv?: boolean; | ||
} | ||
} & TexExtends; | ||
export declare function traceError(_: TexContext, err: Error): void; | ||
@@ -31,0 +31,0 @@ export declare function traceInvalidCommand(cmdName: string, ctx: TexContext): (args: any) => void; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.LaTeXParser = exports.texCommands = exports.expectTheOnlyBrace = exports.expectBraceType = exports.traceInvalidCommand = exports.traceError = exports.LaTeXInvalidCommandError = exports.LaTeXError = exports.BraceType = void 0; | ||
const __1 = require(".."); | ||
@@ -166,2 +167,8 @@ const escape_1 = require("../lib/escape"); | ||
}, | ||
R(ctx, vars, tex) { | ||
if (ctx.underMathEnv) { | ||
return '\\mathbb{R}' + releaseVars(vars, 0); | ||
} | ||
return "\\R" + tex(ctx, new __1.StringStream(releaseVars(vars, 0))); | ||
}, | ||
indent(ctx, vars, tex) { | ||
@@ -313,2 +320,2 @@ if (ctx.underMathEnv) { | ||
exports.LaTeXParser = LaTeXParser; | ||
LaTeXParser.cmdNameRegex = /\\([a-zA-Z_]\w*)/; | ||
LaTeXParser.cmdNameRegex = /\\([a-zA-Z]+)/; |
@@ -1,18 +0,5 @@ | ||
import { Parser, StringStream } from ".."; | ||
import { BlockElement, LinkDefinition, Token } from "../token/token"; | ||
import { commandFunc, TexContext } from "../parser/tex-parser"; | ||
import { CodeBlock, Emphasis, HeaderBlock, Horizontal, HTMLBlock, ImageLink, InlineCode, InlinePlain, LaTeXBlock, Link, ListBlock, MathBlock, Paragraph, Quotes } from "../token/token"; | ||
import { IRenderer, RenderContext } from "../proto"; | ||
export declare type HighlightFunc = (code: string, language: string) => string; | ||
export interface RenderContext { | ||
readonly render: Renderer; | ||
readonly next: () => void; | ||
readonly tokens: Token[]; | ||
linkDefs: { | ||
[linkIdentifier: string]: LinkDefinition; | ||
}; | ||
html: string; | ||
texCtx: TexContext; | ||
} | ||
export declare type RenderMiddleware = (ctx: RenderContext) => void; | ||
export interface RenderOptions { | ||
originStack?: RenderMiddleware[]; | ||
wrapCodeClassTag?: (language: string) => string; | ||
@@ -22,36 +9,37 @@ highlight?: HighlightFunc; | ||
} | ||
export declare class Renderer { | ||
protected parser: Parser; | ||
private stack; | ||
private readonly texCommands; | ||
private latexParser; | ||
export declare class Renderer implements IRenderer { | ||
protected highlight?: HighlightFunc; | ||
protected enableLaTeX?: boolean; | ||
constructor(parser: Parser, opts?: RenderOptions); | ||
addMiddleware(middleware: RenderMiddleware): void; | ||
render(s: StringStream, mdFieldTexCommands?: { | ||
[cn: string]: commandFunc; | ||
}): string; | ||
renderString(s: string): string; | ||
renderElements(ctx: RenderContext, elements: Token[]): void; | ||
protected createLinkMap(ctx: RenderContext): void; | ||
protected handleElements(ctx: RenderContext): void; | ||
protected handleElement(ctx: RenderContext, el: BlockElement): void; | ||
protected renderParagraph(ctx: RenderContext, el: BlockElement): void; | ||
protected renderNewLine(_: RenderContext, __: BlockElement): void; | ||
protected renderQuotes(ctx: RenderContext, el: BlockElement): void; | ||
protected renderListBlock(ctx: RenderContext, el: BlockElement): void; | ||
protected renderHorizontal(ctx: RenderContext, _: BlockElement): void; | ||
protected renderLinkDefinition(_: RenderContext, __: BlockElement): void; | ||
constructor(opts?: RenderOptions); | ||
renderParagraph(ctx: RenderContext, paragraphEl: Paragraph): void; | ||
renderQuotes(ctx: RenderContext, quotesEl: Quotes): void; | ||
renderListBlock(ctx: RenderContext, listBlockEl: ListBlock): void; | ||
renderHorizontal(ctx: RenderContext, _: Horizontal): void; | ||
renderNewLine(): void; | ||
renderLinkDefinition(): void; | ||
wrapCodeClassTag(language: string): string; | ||
protected renderCodeBlock(ctx: RenderContext, el: BlockElement): void; | ||
protected renderHTMLBlock(ctx: RenderContext, el: BlockElement): void; | ||
protected renderHeaderBlock(ctx: RenderContext, el: BlockElement): void; | ||
protected renderInlinePlain(ctx: RenderContext, el: BlockElement): void; | ||
protected renderLink(ctx: RenderContext, el: BlockElement): void; | ||
protected renderImageLink(ctx: RenderContext, el: BlockElement): void; | ||
protected renderMathBlock(ctx: RenderContext, el: BlockElement): void; | ||
protected renderLatexBlock(ctx: RenderContext, el: BlockElement): void; | ||
protected renderEmphasis(ctx: RenderContext, el: BlockElement): void; | ||
protected renderInlineCode(ctx: RenderContext, el: BlockElement): void; | ||
renderCodeBlock(ctx: RenderContext, codeBlockEl: CodeBlock): void; | ||
renderHTMLBlock(ctx: RenderContext, htmlBlockEl: HTMLBlock): void; | ||
renderHeaderBlock(ctx: RenderContext, headerBlockEl: HeaderBlock): void; | ||
renderInlinePlain(ctx: RenderContext, inlinePlainEl: InlinePlain): void; | ||
renderLink(ctx: RenderContext, linkEl: Link): void; | ||
renderImageLink(ctx: RenderContext, imageLinkEl: ImageLink): void; | ||
renderMathBlock(ctx: RenderContext, mathBlockEl: MathBlock): void; | ||
renderLatexBlock(ctx: RenderContext, latexBlockEl: LaTeXBlock): void; | ||
renderEmphasis(ctx: RenderContext, emphasisEl: Emphasis): void; | ||
renderInlineCode(ctx: RenderContext, inlineCodeEl: InlineCode): void; | ||
} | ||
interface LaTeXRendererExt { | ||
sectionCounter: number; | ||
ssCsCounter?: number; | ||
subsectionCounter: number; | ||
renderStack: string[]; | ||
} | ||
export declare class LaTeXRenderer extends Renderer implements IRenderer { | ||
constructor(opts?: RenderOptions); | ||
initContext(ctx: RenderContext<LaTeXRendererExt>): void; | ||
renderHeaderBlock(ctx: RenderContext<LaTeXRendererExt>, headerBlockEl: HeaderBlock): void; | ||
renderParagraph(ctx: RenderContext<LaTeXRendererExt>, paragraphEl: Paragraph): void; | ||
renderListBlock(ctx: RenderContext<LaTeXRendererExt>, listBlockEl: ListBlock): void; | ||
} | ||
export {}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.LaTeXRenderer = exports.Renderer = void 0; | ||
const __1 = require(".."); | ||
const token_1 = require("../token/token"); | ||
const tex_parser_1 = require("../parser/tex-parser"); | ||
const escape_1 = require("../lib/escape"); | ||
class Renderer { | ||
constructor(parser, opts) { | ||
this.parser = parser; | ||
this.stack = (opts === null || opts === void 0 ? void 0 : opts.originStack) || [this.createLinkMap, this.handleElements]; | ||
constructor(opts) { | ||
if (opts) { | ||
@@ -18,128 +15,22 @@ this.highlight = opts.highlight; | ||
} | ||
this.texCommands = tex_parser_1.texCommands; | ||
this.latexParser = new tex_parser_1.LaTeXParser(); | ||
} | ||
// noinspection JSUnusedGlobalSymbols | ||
addMiddleware(middleware) { | ||
this.stack.push(middleware); | ||
} | ||
// noinspection JSUnusedGlobalSymbols | ||
render(s, mdFieldTexCommands) { | ||
let stackIndex = 0; | ||
let ctx = { | ||
render: this, tokens: this.parser.parseBlockElements(s), linkDefs: {}, html: '', texCtx: { | ||
texCommands: this.texCommands, | ||
texCommandDefs: mdFieldTexCommands || {}, | ||
}, next() { | ||
for (; stackIndex < ctx.render.stack.length;) { | ||
ctx.render.stack[stackIndex++](ctx); | ||
} | ||
} | ||
}; | ||
ctx.next(); | ||
return ctx.html; | ||
} | ||
// noinspection JSUnusedGlobalSymbols | ||
renderString(s) { | ||
return this.render(new __1.StringStream(s)); | ||
} | ||
renderElements(ctx, elements) { | ||
for (let el of elements) { | ||
ctx.render.handleElement(ctx, el); | ||
} | ||
} | ||
createLinkMap(ctx) { | ||
for (let el of ctx.tokens) { | ||
if (el.token_type === token_1.TokenType.LinkDefinition) { | ||
let linkDef = el; | ||
ctx.linkDefs[linkDef.linkIdentifier] = linkDef; | ||
} | ||
} | ||
} | ||
handleElements(ctx) { | ||
for (let el of ctx.tokens) { | ||
ctx.render.handleElement(ctx, el); | ||
} | ||
} | ||
handleElement(ctx, el) { | ||
switch (el.token_type) { | ||
case token_1.TokenType.Paragraph: | ||
this.renderParagraph(ctx, el); | ||
break; | ||
case token_1.TokenType.NewLine: | ||
this.renderNewLine(ctx, el); | ||
break; | ||
case token_1.TokenType.Quotes: | ||
this.renderQuotes(ctx, el); | ||
break; | ||
case token_1.TokenType.ListBlock: | ||
this.renderListBlock(ctx, el); | ||
break; | ||
case token_1.TokenType.Horizontal: | ||
this.renderHorizontal(ctx, el); | ||
break; | ||
case token_1.TokenType.LinkDefinition: | ||
this.renderLinkDefinition(ctx, el); | ||
break; | ||
case token_1.TokenType.CodeBlock: | ||
this.renderCodeBlock(ctx, el); | ||
break; | ||
// can latex | ||
case token_1.TokenType.HTMLBlock: | ||
this.renderHTMLBlock(ctx, el); | ||
break; | ||
case token_1.TokenType.HeaderBlock: | ||
this.renderHeaderBlock(ctx, el); | ||
break; | ||
// can latex | ||
case token_1.TokenType.InlinePlain: | ||
this.renderInlinePlain(ctx, el); | ||
break; | ||
// can latex | ||
case token_1.TokenType.Link: | ||
this.renderLink(ctx, el); | ||
break; | ||
case token_1.TokenType.ImageLink: | ||
this.renderImageLink(ctx, el); | ||
break; | ||
case token_1.TokenType.MathBlock: | ||
this.renderMathBlock(ctx, el); | ||
break; | ||
case token_1.TokenType.LatexBlock: | ||
this.renderLatexBlock(ctx, el); | ||
break; | ||
// can latex | ||
case token_1.TokenType.Emphasis: | ||
this.renderEmphasis(ctx, el); | ||
break; | ||
case token_1.TokenType.InlineCode: | ||
this.renderInlineCode(ctx, el); | ||
break; | ||
default: | ||
throw new Error(`invalid Token Type: ${el.token_type}`); | ||
} | ||
} | ||
renderParagraph(ctx, el) { | ||
renderParagraph(ctx, paragraphEl) { | ||
ctx.html += '<p>'; | ||
ctx.render.renderElements(ctx, el.inlineElements); | ||
ctx.driver.renderElements(ctx, paragraphEl.inlineElements); | ||
ctx.html += '</p>'; | ||
} | ||
renderNewLine(_, __) { | ||
// ignore it | ||
} | ||
renderQuotes(ctx, el) { | ||
renderQuotes(ctx, quotesEl) { | ||
ctx.html += '<blockquote>'; | ||
ctx.render.renderElements(ctx, el.insideTokens); | ||
ctx.driver.renderElements(ctx, quotesEl.insideTokens); | ||
ctx.html += '</blockquote>'; | ||
} | ||
renderListBlock(ctx, el) { | ||
let listBlock = el; | ||
ctx.html += '<' + (listBlock.ordered ? 'ol' : 'ul') + '>'; | ||
for (let listEl of listBlock.listElements) { | ||
// omitting listEl.blankSeparated | ||
renderListBlock(ctx, listBlockEl) { | ||
ctx.html += '<' + (listBlockEl.ordered ? 'ol' : 'ul') + '>'; | ||
for (let listEl of listBlockEl.listElements) { | ||
// omitting listEl.lineBreakAttached | ||
ctx.html += '<li>'; | ||
ctx.render.renderElements(ctx, listEl.innerBlocks); | ||
ctx.driver.renderElements(ctx, listEl.innerBlocks); | ||
ctx.html += '</li>'; | ||
} | ||
ctx.html += '</' + (listBlock.ordered ? 'ol' : 'ul') + '>'; | ||
ctx.html += '</' + (listBlockEl.ordered ? 'ol' : 'ul') + '>'; | ||
} | ||
@@ -149,86 +40,127 @@ renderHorizontal(ctx, _) { | ||
} | ||
renderLinkDefinition(_, __) { | ||
renderNewLine() { | ||
// ignore it | ||
} | ||
renderLinkDefinition() { | ||
// ignore it | ||
} | ||
wrapCodeClassTag(language) { | ||
return 'lang-' + language; | ||
} | ||
renderCodeBlock(ctx, el) { | ||
let codeBlock = (el); | ||
renderCodeBlock(ctx, codeBlockEl) { | ||
if (this.highlight) { | ||
codeBlock.body = this.highlight(codeBlock.body, codeBlock.language || ''); | ||
codeBlockEl.body = this.highlight(codeBlockEl.body, codeBlockEl.language || ''); | ||
} | ||
else { | ||
codeBlock.body = escape_1.escapeHTML(codeBlock.body); | ||
codeBlockEl.body = escape_1.escapeHTML(codeBlockEl.body); | ||
} | ||
ctx.html += '<pre><code' + | ||
(codeBlock.language ? (' class="' + escape_1.escapeHTML(this.wrapCodeClassTag(codeBlock.language)) + '"') : '') + '>' + | ||
codeBlock.body + '</code></pre>'; | ||
(codeBlockEl.language ? (' class="' + escape_1.escapeHTML(this.wrapCodeClassTag(codeBlockEl.language)) + '"') : '') + '>' + | ||
codeBlockEl.body + '</code></pre>'; | ||
} | ||
renderHTMLBlock(ctx, el) { | ||
ctx.html += (el).body; | ||
renderHTMLBlock(ctx, htmlBlockEl) { | ||
ctx.html += htmlBlockEl.body; | ||
} | ||
renderHeaderBlock(ctx, el) { | ||
let headerBlock = el; | ||
ctx.html += "<h" + headerBlock.level + ">"; | ||
ctx.render.renderElements(ctx, headerBlock.inlineElements); | ||
ctx.html += "</h" + headerBlock.level + ">"; | ||
renderHeaderBlock(ctx, headerBlockEl) { | ||
ctx.html += "<h" + headerBlockEl.level + ">"; | ||
ctx.driver.renderElements(ctx, headerBlockEl.inlineElements); | ||
ctx.html += "</h" + headerBlockEl.level + ">"; | ||
} | ||
renderInlinePlain(ctx, el) { | ||
renderInlinePlain(ctx, inlinePlainEl) { | ||
ctx.texCtx.underMathEnv = false; | ||
ctx.html += this.enableLaTeX ? | ||
this.latexParser.tex(ctx.texCtx, new __1.StringStream((el).content)) : | ||
escape_1.escapeHTML((el).content); | ||
ctx.latexParser.tex(ctx.texCtx, new __1.StringStream(inlinePlainEl.content)) : | ||
escape_1.escapeHTML(inlinePlainEl.content); | ||
} | ||
renderLink(ctx, el) { | ||
let link = el; | ||
if (!link.inlineOrReference) { | ||
if (ctx.linkDefs.hasOwnProperty(link.link)) { | ||
link.link = ctx.linkDefs[link.link].url; | ||
link.inlineOrReference = true; | ||
renderLink(ctx, linkEl) { | ||
if (!linkEl.inlineOrReference) { | ||
if (ctx.linkDefs.hasOwnProperty(linkEl.link)) { | ||
linkEl.link = ctx.linkDefs[linkEl.link].url; | ||
linkEl.inlineOrReference = true; | ||
} | ||
} | ||
ctx.html += '<a href="' + link.link + '"'; | ||
if (link.title) { | ||
ctx.html += ' title="' + escape_1.escapeHTML(link.title) + '"'; | ||
ctx.html += '<a href="' + linkEl.link + '"'; | ||
if (linkEl.title) { | ||
ctx.html += ' title="' + escape_1.escapeHTML(linkEl.title) + '"'; | ||
} | ||
ctx.texCtx.underMathEnv = false; | ||
ctx.html += '>' + ((this.enableLaTeX ? this.latexParser.tex(ctx.texCtx, new __1.StringStream(link.linkTitle)) : | ||
escape_1.escapeHTML(link.linkTitle))) + '</a>'; | ||
ctx.html += '>' + ((this.enableLaTeX ? ctx.latexParser.tex(ctx.texCtx, new __1.StringStream(linkEl.linkTitle)) : | ||
escape_1.escapeHTML(linkEl.linkTitle))) + '</a>'; | ||
} | ||
renderImageLink(ctx, el) { | ||
let link = el; | ||
if (!link.inlineOrReference) { | ||
if (ctx.linkDefs.hasOwnProperty(link.link)) { | ||
link.link = ctx.linkDefs[link.link].url; | ||
link.inlineOrReference = true; | ||
renderImageLink(ctx, imageLinkEl) { | ||
if (!imageLinkEl.inlineOrReference) { | ||
if (ctx.linkDefs.hasOwnProperty(imageLinkEl.link)) { | ||
imageLinkEl.link = ctx.linkDefs[imageLinkEl.link].url; | ||
imageLinkEl.inlineOrReference = true; | ||
} | ||
} | ||
ctx.html += '<img src="' + link.link + '"' + '" alt="' + escape_1.escapeHTML(link.linkTitle) + '"' + | ||
(link.title ? ' title="' + escape_1.escapeHTML(link.title) + '"' : '') + | ||
ctx.html += '<img src="' + imageLinkEl.link + '"' + '" alt="' + escape_1.escapeHTML(imageLinkEl.linkTitle) + '"' + | ||
(imageLinkEl.title ? ' title="' + escape_1.escapeHTML(imageLinkEl.title) + '"' : '') + | ||
"/>"; | ||
} | ||
renderMathBlock(ctx, el) { | ||
let mathBlock = el; | ||
renderMathBlock(ctx, mathBlockEl) { | ||
ctx.texCtx.underMathEnv = true; | ||
ctx.html += '<script type="math/tex' + (mathBlock.inline ? '' : '; mode=display') + '">' + (this.enableLaTeX ? this.latexParser.tex(ctx.texCtx, new __1.StringStream(mathBlock.content)) : | ||
mathBlock.content) + '</script>'; | ||
ctx.html += '<script type="math/tex' + (mathBlockEl.inline ? '' : '; mode=display') + '">' + (this.enableLaTeX ? ctx.latexParser.tex(ctx.texCtx, new __1.StringStream(mathBlockEl.content)) : | ||
mathBlockEl.content) + '</script>'; | ||
} | ||
renderLatexBlock(ctx, el) { | ||
let latexBlock = el; | ||
renderLatexBlock(ctx, latexBlockEl) { | ||
ctx.texCtx.underMathEnv = false; | ||
ctx.html += this.latexParser.tex(ctx.texCtx, new __1.StringStream(latexBlock.content)); | ||
ctx.html += ctx.latexParser.tex(ctx.texCtx, new __1.StringStream(latexBlockEl.content)); | ||
} | ||
renderEmphasis(ctx, el) { | ||
let emphasisEl = el; | ||
renderEmphasis(ctx, emphasisEl) { | ||
ctx.texCtx.underMathEnv = false; | ||
ctx.html += (emphasisEl.level === 2 ? '<strong>' : '<em>') + | ||
(this.enableLaTeX ? this.latexParser.tex(ctx.texCtx, new __1.StringStream(emphasisEl.content)) : | ||
(this.enableLaTeX ? ctx.latexParser.tex(ctx.texCtx, new __1.StringStream(emphasisEl.content)) : | ||
escape_1.escapeHTML(emphasisEl.content)) + | ||
(emphasisEl.level === 2 ? '</strong>' : '</em>'); | ||
} | ||
renderInlineCode(ctx, el) { | ||
ctx.html += '<code>' + escape_1.escapeHTML(el.content) + '</code>'; | ||
renderInlineCode(ctx, inlineCodeEl) { | ||
ctx.html += '<code>' + escape_1.escapeHTML(inlineCodeEl.content) + '</code>'; | ||
} | ||
} | ||
exports.Renderer = Renderer; | ||
class LaTeXRenderer extends Renderer { | ||
constructor(opts) { | ||
super(opts); | ||
} | ||
initContext(ctx) { | ||
ctx.texCtx.sectionCounter = 0; | ||
ctx.texCtx.subsectionCounter = 0; | ||
ctx.texCtx.ssCsCounter = undefined; | ||
ctx.texCtx.renderStack = []; | ||
} | ||
renderHeaderBlock(ctx, headerBlockEl) { | ||
ctx.html += "<h" + headerBlockEl.level + '>'; | ||
// todo: section => h[optK], subsection => h[optKK] | ||
if (headerBlockEl.level === 1) { | ||
ctx.texCtx.sectionCounter++; | ||
ctx.html += '<span class="section-number">' + ctx.texCtx.sectionCounter.toString() + '</span>'; | ||
} | ||
else if (headerBlockEl.level === 3) { | ||
// undefined => 0 | ||
if (ctx.texCtx.ssCsCounter != ctx.texCtx.sectionCounter) { | ||
ctx.texCtx.subsectionCounter = 0; | ||
ctx.texCtx.ssCsCounter = ctx.texCtx.sectionCounter; | ||
} | ||
ctx.texCtx.subsectionCounter++; | ||
ctx.html += '<span class="section-number">' + ctx.texCtx.sectionCounter.toString() | ||
+ '.' + ctx.texCtx.subsectionCounter.toString() + '</span>'; | ||
} | ||
ctx.driver.renderElements(ctx, headerBlockEl.inlineElements); | ||
ctx.html += "</h" + headerBlockEl.level + ">"; | ||
} | ||
renderParagraph(ctx, paragraphEl) { | ||
ctx.html += '<p>'; | ||
if (!(ctx.texCtx.renderStack && ctx.texCtx.renderStack.indexOf('list') !== -1)) { | ||
ctx.html += '<span class="indent"></span>'; | ||
} | ||
ctx.driver.renderElements(ctx, paragraphEl.inlineElements); | ||
ctx.html += '</p>'; | ||
} | ||
renderListBlock(ctx, listBlockEl) { | ||
ctx.texCtx.renderStack.push('list'); | ||
super.renderListBlock(ctx, listBlockEl); | ||
ctx.texCtx.renderStack.pop(); | ||
} | ||
} | ||
exports.LaTeXRenderer = LaTeXRenderer; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.newRules = exports.newInlineRules = exports.newBlockRules = exports.blockRules = exports.inlineRules = void 0; | ||
const std_1 = require("./rules/std"); | ||
@@ -24,3 +25,3 @@ const latex_1 = require("./rules/latex"); | ||
let rules2 = [ | ||
new latex_1.ParagraphRule({ skipLaTeXBlock: (opts === null || opts === void 0 ? void 0 : opts.enableLaTeX) || false, skipMathBlock: true }), | ||
new latex_1.ParagraphRule(), | ||
]; | ||
@@ -27,0 +28,0 @@ // default enable |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.GFMFencedCodeBlockRule = void 0; | ||
const stream_1 = require("../lib/stream"); | ||
@@ -4,0 +5,0 @@ const token_1 = require("../token/token"); |
import { StringStream } from "../lib/stream"; | ||
import { Rule, RuleContext } from "./rule"; | ||
import { MaybeToken } from "../token/token"; | ||
import { MaybeF } from "../lib/fp"; | ||
export declare class InlineLatexCommandRule implements Rule { | ||
@@ -31,34 +30,2 @@ readonly name: string; | ||
} | ||
export declare class ParagraphRule implements Rule { | ||
readonly name: string; | ||
readonly description: string; | ||
protected readonly detect: MaybeF<{ | ||
lastChar: string; | ||
s: StringStream; | ||
i: number; | ||
}>; | ||
constructor(opts?: { | ||
skipLaTeXBlock: boolean; | ||
skipMathBlock: boolean; | ||
}); | ||
match(s: StringStream, ctx: RuleContext): MaybeToken; | ||
fastMatch(s: StringStream, ctx: RuleContext): MaybeToken; | ||
protected detectMathBlock(g: { | ||
lastChar: string; | ||
s: StringStream; | ||
i: number; | ||
}): { | ||
lastChar: string; | ||
s: StringStream; | ||
i: number; | ||
} | undefined; | ||
protected detectLaTeXBlock(g: { | ||
lastChar: string; | ||
s: StringStream; | ||
i: number; | ||
}): { | ||
lastChar: string; | ||
s: StringStream; | ||
i: number; | ||
} | undefined; | ||
} | ||
export { ParagraphRule } from './std'; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.MathBlockRule = exports.InlineMathRule = exports.LatexBlockRule = exports.InlineLatexCommandRule = void 0; | ||
const stream_1 = require("../lib/stream"); | ||
const token_1 = require("../token/token"); | ||
const fp_1 = require("../lib/fp"); | ||
function _braceMatch(s, l, r) { | ||
@@ -72,3 +72,3 @@ if (s.source[0] == l) { | ||
stream_1.forwardRegexp(s, capturing); | ||
return new token_1.LateXBlock(capturing[0] + this.braceMatch(s)); | ||
return new token_1.LaTeXBlock(capturing[0] + this.braceMatch(s)); | ||
} | ||
@@ -136,115 +136,3 @@ braceMatch(s) { | ||
exports.MathBlockRule = MathBlockRule; | ||
class ParagraphRule { | ||
constructor(opts) { | ||
this.name = "Paragraph"; | ||
this.description = "Standard Markdown Block Rule"; | ||
let detectors = []; | ||
if (opts && opts.skipMathBlock) { | ||
detectors.push(this.detectMathBlock); | ||
} | ||
if (opts && opts.skipLaTeXBlock) { | ||
detectors.push(this.detectLaTeXBlock); | ||
} | ||
if (detectors.length !== 2) { | ||
this.detect = fp_1.maybeCompose(...detectors); | ||
} | ||
else { | ||
this.detect = this.detectMathBlock; | ||
this.match = this.fastMatch; | ||
} | ||
} | ||
match(s, ctx) { | ||
let g = { lastChar: '\xff', s: s, i: 0 }; | ||
if (s.source[0] == '\n') { | ||
return undefined; | ||
} | ||
for (; g.i < s.source.length; g.i++) { | ||
if (g.lastChar === '\n') { | ||
if (('\n' === s.source[g.i]) || | ||
// ('\t' === s.source[g.i]) || (s.source[g.i] === ' ' && | ||
// g.i + 3 < s.source.length && s.source[g.i + 1] === ' ' && | ||
// s.source[g.i + 2] === ' ' && s.source[g.i + 3] === ' ') || | ||
('*+-'.includes(s.source[g.i]) && (g.i + 1 < s.source.length && s.source[g.i + 1] === ' '))) { | ||
g.i--; | ||
break; | ||
} | ||
} | ||
if (this.detect(g) === undefined) { | ||
break; | ||
} | ||
if (g.lastChar === '\\' && s.source[g.i] !== '\n') { | ||
g.lastChar = 'a'; | ||
} | ||
else { | ||
g.lastChar = s.source[g.i]; | ||
} | ||
} | ||
if (!g.i) { | ||
return undefined; | ||
} | ||
let capturing = s.source.slice(0, g.i); | ||
s.forward(g.i); | ||
return new token_1.Paragraph(ctx.parseInlineElements(new stream_1.StringStream(capturing))); | ||
} | ||
fastMatch(s, ctx) { | ||
let lastChar = 'a', i = 0; | ||
if (s.source[0] == '\n') { | ||
return undefined; | ||
} | ||
for (; i < s.source.length; i++) { | ||
// noinspection DuplicatedCode | ||
if (lastChar === '\n') { | ||
if (('\n' === s.source[i]) || | ||
// ('\t' === s.source[i]) || (s.source[i] === ' ' && | ||
// i + 3 < s.source.length && s.source[i + 1] === ' ' && | ||
// s.source[i + 2] === ' ' && s.source[i + 3] === ' ') || | ||
('*+-'.includes(s.source[i]) && (i + 1 < s.source.length && s.source[i + 1] === ' '))) { | ||
i--; | ||
break; | ||
} | ||
} | ||
if (lastChar === s.source[i] && (lastChar === '$')) { | ||
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]; | ||
} | ||
} | ||
if (!i) { | ||
return undefined; | ||
} | ||
let capturing = s.source.slice(0, i); | ||
s.forward(i); | ||
return new token_1.Paragraph(ctx.parseInlineElements(new stream_1.StringStream(capturing))); | ||
} | ||
detectMathBlock(g) { | ||
if (g.lastChar === g.s.source[g.i] && g.lastChar === '$') { | ||
g.i--; | ||
return undefined; | ||
} | ||
return g; | ||
} | ||
detectLaTeXBlock(g) { | ||
if (g.lastChar === '\n' && g.s.source[g.i] === '\\') { | ||
let nextIndex = g.i + 1, nextChar = g.s.source[nextIndex]; | ||
if (nextIndex < g.s.source.length && | ||
(('a' <= nextChar && nextChar <= 'z') || ('A' <= nextChar && nextChar <= 'Z'))) { | ||
g.i--; | ||
return undefined; | ||
} | ||
} | ||
return g; | ||
} | ||
} | ||
exports.ParagraphRule = ParagraphRule; | ||
var std_1 = require("./std"); | ||
Object.defineProperty(exports, "ParagraphRule", { enumerable: true, get: function () { return std_1.ParagraphRule; } }); |
@@ -13,5 +13,3 @@ import { Rule, RuleContext } from "./rule"; | ||
readonly description: string; | ||
constructor({ otherBlockBegin }: { | ||
otherBlockBegin: string[]; | ||
}); | ||
constructor(); | ||
match(s: StringStream, ctx: RuleContext): MaybeToken; | ||
@@ -57,2 +55,14 @@ } | ||
static readonly listBlockRegex: RegExp; | ||
/** | ||
* undefined behavior: | ||
* + a | ||
* [ ]{2} | ||
* + 1 | ||
* | ||
* will parsed into: | ||
* {+ }{a | ||
* [ ]{2}} | ||
* {+ }{1} | ||
*/ | ||
static readonly matchIndentedBody: RegExp; | ||
static readonly replaceRegex: RegExp; | ||
@@ -59,0 +69,0 @@ private readonly enableGFMRules; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.InlineCodeRule = exports.EmphasisRule = exports.LinkOrImageRule = exports.InlinePlainRule = exports.InlinePlainExceptSpecialMarksRule = exports.HTMLBlockRule = exports.ListBlockRule = exports.LinkDefinitionRule = exports.HorizontalRule = exports.HeaderBlockRule = exports.CodeBlockRule = exports.QuotesRule = exports.ParagraphRule = exports.NewLineRule = void 0; | ||
const stream_1 = require("../lib/stream"); | ||
@@ -43,3 +44,3 @@ const token_1 = require("../token/token"); | ||
// public readonly regex: RegExp = /^(?:(?:[^$]|\$(?!\$))(?:\n|$)?)+/; | ||
constructor({ otherBlockBegin = [] }) { | ||
constructor() { | ||
this.name = "Paragraph"; | ||
@@ -49,28 +50,9 @@ this.description = "Standard Markdown Block Rule"; | ||
match(s, ctx) { | ||
let lastChar = 'a', i = 0; | ||
if (s.source[0] == '\n') { | ||
return undefined; | ||
} | ||
for (; i < s.source.length; i++) { | ||
// noinspection DuplicatedCode | ||
if (lastChar === '\n') { | ||
if (('\n' === s.source[i]) || | ||
// ('\t' === s.source[i]) || (s.source[i] === ' ' && | ||
// i + 3 < s.source.length && s.source[i + 1] === ' ' && | ||
// s.source[i + 2] === ' ' && s.source[i + 3] === ' ') || | ||
('*+-'.includes(s.source[i]) && (i + 1 < s.source.length && s.source[i + 1] === ' '))) { | ||
i--; | ||
break; | ||
} | ||
} | ||
if (lastChar === '\\' && s.source[i] !== '\n') { | ||
lastChar = 'a'; | ||
} | ||
else { | ||
lastChar = s.source[i]; | ||
} | ||
const i = s.source.indexOf('\n'); | ||
if (i === -1) { | ||
return new token_1.Paragraph(ctx.parseInlineElements(s)); | ||
} | ||
if (!i) { | ||
return undefined; | ||
} | ||
let capturing = s.source.slice(0, i); | ||
@@ -179,2 +161,9 @@ s.forward(i); | ||
exports.LinkDefinitionRule = LinkDefinitionRule; | ||
var ListBlockMatchState; | ||
(function (ListBlockMatchState) { | ||
ListBlockMatchState[ListBlockMatchState["FindStartNumber"] = 0] = "FindStartNumber"; | ||
ListBlockMatchState[ListBlockMatchState["PushListElement"] = 1] = "PushListElement"; | ||
ListBlockMatchState[ListBlockMatchState["MatchBodyLine"] = 2] = "MatchBodyLine"; | ||
ListBlockMatchState[ListBlockMatchState["Final"] = 3] = "Final"; | ||
})(ListBlockMatchState || (ListBlockMatchState = {})); | ||
class ListBlockRule { | ||
@@ -187,13 +176,108 @@ constructor({ enableGFMRules }) { | ||
match(s, ctx) { | ||
let ordered; | ||
if ("*+-".includes(s.source[0])) { | ||
ordered = false; | ||
let lookAheadRegex = /^[\t\r\v\f ]{0,3}([*+-]|[0-9]+\.)[\t\r\v\f ]/; | ||
let capturing = lookAheadRegex.exec(s.source); | ||
if (!capturing) { | ||
return undefined; | ||
} | ||
else if ('0' <= s.source[0] && s.source[0] <= '9') { | ||
ordered = true; | ||
let currMarker = capturing[1]; | ||
stream_1.forwardRegexp(s, capturing); | ||
let newLineSeparated = false; | ||
let maybeNewlineSeparated = false; | ||
let nextMarker = ''; | ||
let pleNextState = ListBlockMatchState.Final; | ||
const listBodies = []; | ||
const matchFullLineBody = () => { | ||
let readPtr = 0; | ||
while (readPtr < s.source.length && s.at(readPtr) !== '\n') { | ||
readPtr++; | ||
} | ||
maybeNewlineSeparated = readPtr < s.source.length; | ||
if (maybeNewlineSeparated) { | ||
readPtr++; | ||
} | ||
if (readPtr) { | ||
listBodies.push(s.source.slice(0, readPtr)); | ||
s.forward(readPtr); | ||
} | ||
}; | ||
matchFullLineBody(); | ||
const ordered = '0' <= currMarker && currMarker <= '9'; | ||
const listBlockEl = new token_1.ListBlock(ordered); | ||
if (ordered) { | ||
currMarker = currMarker.slice(0, currMarker.length - 1); | ||
lookAheadRegex = /^[\t\r\v\f ]?([0-9]+)\.[\t\r\v\f ]/; | ||
} | ||
else { | ||
return undefined; | ||
lookAheadRegex = /^[\t\r\v\f ]?([*+-])[\t\r\v\f ]/; | ||
} | ||
return this.matchBlock(new token_1.ListBlock(ordered), s, ctx); | ||
let state = ListBlockMatchState.MatchBodyLine; | ||
for (;;) { | ||
switch (state) { | ||
case ListBlockMatchState.FindStartNumber: | ||
capturing = lookAheadRegex.exec(s.source); | ||
if (!capturing) { | ||
nextMarker = ""; | ||
if (newLineSeparated) { | ||
state = currMarker.length ? ListBlockMatchState.PushListElement : ListBlockMatchState.Final; | ||
} | ||
else { | ||
matchFullLineBody(); | ||
if (maybeNewlineSeparated) { | ||
state = ListBlockMatchState.MatchBodyLine; | ||
} | ||
else { | ||
state = currMarker.length ? ListBlockMatchState.PushListElement : ListBlockMatchState.Final; | ||
} | ||
} | ||
} | ||
else { | ||
nextMarker = capturing[1]; | ||
stream_1.forwardRegexp(s, capturing); | ||
state = ListBlockMatchState.PushListElement; | ||
pleNextState = ListBlockMatchState.MatchBodyLine; | ||
} | ||
break; | ||
case ListBlockMatchState.PushListElement: | ||
listBlockEl.listElements.push(new token_1.ListElement(currMarker, ctx.parseBlockElements(new stream_1.StringStream(listBodies.join('').replace(ListBlockRule.replaceRegex, ''))), newLineSeparated, undefined)); | ||
currMarker = nextMarker; | ||
listBodies.splice(0, listBodies.length); | ||
if (nextMarker.length) { | ||
matchFullLineBody(); | ||
// match end | ||
if (!maybeNewlineSeparated) { | ||
currMarker = nextMarker; | ||
nextMarker = ''; | ||
if (currMarker.length) { | ||
pleNextState = ListBlockMatchState.PushListElement; | ||
newLineSeparated = false; | ||
} | ||
else { | ||
pleNextState = ListBlockMatchState.Final; | ||
} | ||
} | ||
} | ||
state = pleNextState; | ||
pleNextState = ListBlockMatchState.Final; | ||
break; | ||
case ListBlockMatchState.MatchBodyLine: | ||
capturing = ListBlockRule.matchIndentedBody.exec(s.source); | ||
if (capturing[0].length) { | ||
listBodies.push(capturing[0]); | ||
maybeNewlineSeparated = capturing[0].endsWith('\n'); | ||
stream_1.forwardRegexp(s, capturing); | ||
} | ||
capturing = (/^\s*/m.exec(s.source)); | ||
if (maybeNewlineSeparated) { | ||
newLineSeparated = capturing[0].indexOf('\n') !== -1; | ||
} | ||
stream_1.forwardRegexp(s, capturing); | ||
maybeNewlineSeparated = false; | ||
state = ListBlockMatchState.FindStartNumber; | ||
break; | ||
case ListBlockMatchState.Final: | ||
return listBlockEl; | ||
default: | ||
throw new Error(`unknown state: ${state}`); | ||
} | ||
} | ||
} | ||
@@ -209,11 +293,19 @@ ; | ||
for (blockContent = '', marker = nextMarker, nextMarker = undefined; marker !== undefined; blockContent = '', marker = nextMarker, nextMarker = undefined) { | ||
do { | ||
let capturing = ListBlockRule.listBlockRegex.exec(s.source); | ||
if (capturing === null) { | ||
throw s.wrapErr(new Error("match block failed")); | ||
} | ||
stream_1.forwardRegexp(s, capturing); | ||
blockContent += capturing[0]; | ||
} while (!s.eof && !l.lookAhead0(s) && (nextMarker = l.lookAhead(s)) === undefined); | ||
blockContent = blockContent.replace(ListBlockRule.replaceRegex, ''); | ||
// do { | ||
// let capturing = ListBlockRule.listBlockRegex.exec(s.source); | ||
// if (capturing === null) { | ||
// throw s.wrapErr(new Error("match block failed")); | ||
// } | ||
// forwardRegexp(s, capturing); | ||
// blockContent += capturing[0]; | ||
// | ||
// | ||
// } while (!s.eof && !l.lookAhead0(s) && (nextMarker = l.lookAhead(s)) === undefined); | ||
let capturing = ListBlockRule.listBlockRegex.exec(s.source); | ||
if (capturing === null) { | ||
throw s.wrapErr(new Error("match block failed")); | ||
} | ||
stream_1.forwardRegexp(s, capturing); | ||
// blockContent += capturing[0]; | ||
blockContent = capturing[0].replace(ListBlockRule.replaceRegex, ''); | ||
let sep = blockContent.endsWith('\n\n'); | ||
@@ -247,4 +339,17 @@ if (sep) { | ||
//^((?:[^\n]+(?:\n|$)(?=[^0-9*+-])(\n(?=[^0-9*+-]))?)+) | ||
ListBlockRule.listBlockRegex = /^(?:(?:[^\n]*)(?:\n|$)\n?)(?:(?=[^0-9*+-])(?:[^\n]+)(?:\n|$)\n?)*/; | ||
ListBlockRule.replaceRegex = /^(?: {4}|\t)/gm; | ||
//^(?:(?:[^\n]*)(?:\n|$)\n?)(?:(?=[^0-9*+-])(?:[^\n]+)(?:\n|$)\n?)* | ||
ListBlockRule.listBlockRegex = /^(?:(?:[^\n]*)(?:\n|$))(?:(?=\n?[^0-9*+-])(?:[^\n]+)(?:\n|$))*/; | ||
/** | ||
* undefined behavior: | ||
* + a | ||
* [ ]{2} | ||
* + 1 | ||
* | ||
* will parsed into: | ||
* {+ }{a | ||
* [ ]{2}} | ||
* {+ }{1} | ||
*/ | ||
ListBlockRule.matchIndentedBody = /^(?:\s*?([\r\t\f\v ]{2,}[^\n]*(?:\n|$)))*/; | ||
ListBlockRule.replaceRegex = /^(?: {2}|\t)/gm; | ||
function defaultValidTags() { | ||
@@ -251,0 +356,0 @@ ///^<([a-zA-Z][a-zA-Z0-9-]*)(?:\s+(?:[a-zA-Z_:][a-zA-Z0-9_.:-]*)(?:\s*=\s*(?:[^\/\s'"=<>`]|'[^']*'|"[^"]*"))?)*\s*(\/?)>/ |
@@ -47,5 +47,5 @@ import { StringStream } from ".."; | ||
marker: string; | ||
blankSeparated: boolean; | ||
lineBreakAttached: boolean; | ||
selector?: string; | ||
constructor(marker: string, innerBlocks?: BlockElement[], blankSeparated?: boolean, selector?: string); | ||
constructor(marker: string, innerBlocks?: BlockElement[], lineBreakAttached?: boolean, selector?: string); | ||
} | ||
@@ -90,2 +90,14 @@ declare class ListBlock implements BlockElement { | ||
} | ||
declare class LaTeXBlock implements BlockElement { | ||
readonly token_type = TokenType.LatexBlock; | ||
content: string; | ||
inline?: boolean; | ||
constructor(content: string); | ||
} | ||
declare class MathBlock implements BlockElement { | ||
readonly token_type = TokenType.MathBlock; | ||
content: string; | ||
inline?: boolean; | ||
constructor(content: string, inline: boolean); | ||
} | ||
declare class InlinePlain implements InlineElement { | ||
@@ -123,15 +135,3 @@ readonly token_type = TokenType.InlinePlain; | ||
} | ||
declare class LateXBlock implements InlineElement { | ||
readonly token_type = TokenType.LatexBlock; | ||
content: string; | ||
inline?: boolean; | ||
constructor(content: string); | ||
} | ||
declare class MathBlock implements InlineElement { | ||
readonly token_type = TokenType.MathBlock; | ||
content: string; | ||
inline?: boolean; | ||
constructor(content: string, inline: boolean); | ||
} | ||
export { TokenType, Token, BlockElement, InlineElement, ListElement, MaybeToken, }; | ||
export { Paragraph, NewLine, Quotes, ListBlock, Horizontal, LinkDefinition, CodeBlock, HTMLBlock, HeaderBlock, MathBlock, LateXBlock, InlinePlain, Link, ImageLink, Emphasis, InlineCode, StdBlockTokenCount, StdInlineTokenCount, }; | ||
export { Paragraph, NewLine, Quotes, ListBlock, Horizontal, LinkDefinition, CodeBlock, HTMLBlock, HeaderBlock, MathBlock, LaTeXBlock, InlinePlain, Link, ImageLink, Emphasis, InlineCode, StdBlockTokenCount, StdInlineTokenCount, }; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.StdInlineTokenCount = exports.StdBlockTokenCount = exports.InlineCode = exports.Emphasis = exports.ImageLink = exports.Link = exports.InlinePlain = exports.LaTeXBlock = exports.MathBlock = exports.HeaderBlock = exports.HTMLBlock = exports.CodeBlock = exports.LinkDefinition = exports.Horizontal = exports.ListBlock = exports.Quotes = exports.NewLine = exports.Paragraph = exports.ListElement = exports.TokenType = void 0; | ||
var TokenType; | ||
@@ -100,6 +101,6 @@ (function (TokenType) { | ||
class ListElement { | ||
constructor(marker, innerBlocks = [], blankSeparated = false, selector) { | ||
constructor(marker, innerBlocks = [], lineBreakAttached = false, selector) { | ||
this.marker = marker; | ||
this.innerBlocks = innerBlocks; | ||
this.blankSeparated = blankSeparated; | ||
this.lineBreakAttached = lineBreakAttached; | ||
this.selector = selector; | ||
@@ -222,9 +223,15 @@ } | ||
return false; | ||
let fwdPtr = 0; | ||
if (s.source[0] === '\n') { | ||
if (s.source.length <= 1) | ||
return false; | ||
fwdPtr = 1; | ||
} | ||
if (this.ordered) { | ||
return '0' <= s.source[0] && s.source[0] <= '9'; | ||
return '0' <= s.source[fwdPtr] && s.source[fwdPtr] <= '9'; | ||
} | ||
else if ('*+-'.includes(s.source[0])) { | ||
else if ('*+-'.includes(s.source[fwdPtr])) { | ||
let j = 0; | ||
for (let i = 0; s.source[i] && s.source[i] != '\n'; i++) { | ||
if (s.source[i] == s.source[0]) { | ||
for (let i = fwdPtr; s.source[i] && s.source[i] != '\n'; i++) { | ||
if (s.source[i] == s.source[fwdPtr]) { | ||
j++; | ||
@@ -242,3 +249,11 @@ } | ||
static lookAheadOrderedListNumber(s) { | ||
for (let i = 0;; i++) { | ||
if (s.eof) | ||
return undefined; | ||
let fwdPtr = 0; | ||
if (s.source[0] === '\n') { | ||
if (s.source.length <= 1) | ||
return undefined; | ||
fwdPtr = 1; | ||
} | ||
for (let i = fwdPtr; i < s.source.length; i++) { | ||
if ('0' <= s.source[i] && s.source[i] <= '9') { | ||
@@ -254,6 +269,15 @@ continue; | ||
} | ||
return undefined; | ||
} | ||
static lookAheadUnorderedListMarker(s) { | ||
if ('*+-'.includes(s.source[0]) && s.source[1] == ' ') { | ||
let m = s.source[0]; | ||
if (s.eof) | ||
return undefined; | ||
let fwdPtr = 0; | ||
if (s.source[0] === '\n') { | ||
if (s.source.length <= 1) | ||
return undefined; | ||
fwdPtr = 1; | ||
} | ||
if ('*+-'.includes(s.source[fwdPtr]) && s.source[fwdPtr + 1] == ' ') { | ||
let m = s.source[fwdPtr]; | ||
s.forward(2); | ||
@@ -380,2 +404,17 @@ return m; | ||
exports.HeaderBlock = HeaderBlock; | ||
class LaTeXBlock { | ||
constructor(content) { | ||
this.token_type = TokenType.LatexBlock; | ||
this.content = content; | ||
} | ||
} | ||
exports.LaTeXBlock = LaTeXBlock; | ||
class MathBlock { | ||
constructor(content, inline) { | ||
this.token_type = TokenType.MathBlock; | ||
this.content = content; | ||
this.inline = inline; | ||
} | ||
} | ||
exports.MathBlock = MathBlock; | ||
class InlinePlain { | ||
@@ -423,16 +462,1 @@ constructor(content) { | ||
exports.InlineCode = InlineCode; | ||
class LateXBlock { | ||
constructor(content) { | ||
this.token_type = TokenType.LatexBlock; | ||
this.content = content; | ||
} | ||
} | ||
exports.LateXBlock = LateXBlock; | ||
class MathBlock { | ||
constructor(content, inline) { | ||
this.token_type = TokenType.MathBlock; | ||
this.content = content; | ||
this.inline = inline; | ||
} | ||
} | ||
exports.MathBlock = MathBlock; |
{ | ||
"name": "marktex.js", | ||
"version": "0.3.7", | ||
"version": "0.4.1-2", | ||
"description": "markdown parser", | ||
@@ -12,4 +12,4 @@ "main": "dist/index.js", | ||
"prepublish": "yarn run build", | ||
"t": "./node_modules/.bin/mocha.cmd --reporter spec --require ts-node/register", | ||
"b": "./node_modules/.bin/mocha.cmd --prof --reporter spec --require ts-node/register", | ||
"t": "npx mocha --reporter spec --require ts-node/register", | ||
"b": "npx mocha --prof --reporter spec --require ts-node/register", | ||
"bench": "yarn run b src/**/*.bench.ts", | ||
@@ -25,5 +25,7 @@ "test": "yarn run t src/**/*.test.ts" | ||
"@types/mocha": "^7.0.2", | ||
"@types/node": "^14.14.34", | ||
"benchmark": "^2.1.4", | ||
"chai": "^4.2.0", | ||
"mocha": "^7.1.0", | ||
"minimist": ">=0.2.1", | ||
"mocha": "^8.3.2", | ||
"ts-node": "^8.6.2", | ||
@@ -30,0 +32,0 @@ "typescript": "^3.8.3" |
@@ -16,3 +16,3 @@ # MarkTeX | ||
```typescript | ||
# es style | ||
// es style | ||
import myriad from 'marktex.js'; | ||
@@ -22,3 +22,3 @@ // or | ||
# commonJs style | ||
// commonJs style | ||
var {myriad} = require("marktex.js") | ||
@@ -32,3 +32,3 @@ // or | ||
```typescript | ||
console.log(myriad.newRenderer().renderString('## hello marktex.js')); | ||
console.log(myriad.newRenderDriver().renderString('## hello marktex.js')); | ||
``` | ||
@@ -35,0 +35,0 @@ |
@@ -19,2 +19,3 @@ { | ||
"dist" | ||
// "scripts/compiler.ts" | ||
], | ||
@@ -21,0 +22,0 @@ "include": [ |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
107285
10
40
2785
1