Skip to content
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @license
* Copyright (c) Noldova.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*/

export class MarkdownCloser {
private readonly find: (from: number) => number;
private searched: number = Number.POSITIVE_INFINITY;
private found: number = -1;
private none: number = Number.POSITIVE_INFINITY;

public constructor(find: (from: number) => number) {
this.find = find;
}

public at(from: number): number {
if (from >= this.none)
return -1;
if (from >= this.searched && from <= this.found)
return this.found;
const found = this.find(from);
if (found < 0)
this.none = from;
else {
this.searched = from;
this.found = found;
}
return found;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* @license
* Copyright (c) Noldova.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*/

export class MarkdownHtmlBlock {
public readonly start: RegExp;
public readonly end: RegExp | null;

public constructor(start: RegExp, end: RegExp | null) {
this.start = start;
this.end = end;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @license
* Copyright (c) Noldova.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*/

import type { MarkdownTextRun } from "./markdown-text-run.js";

export class MarkdownHtmlOpening {
public readonly run: MarkdownTextRun;
public readonly start: number;
public readonly depth: number;
public readonly peers: MarkdownHtmlOpening[];

public constructor(run: MarkdownTextRun, start: number, depth: number, peers: MarkdownHtmlOpening[]) {
this.run = run;
this.start = start;
this.depth = depth;
this.peers = peers;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* @license
* Copyright (c) Noldova.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*/

import "@noldova/context.core";

import { MarkdownCloser } from "./markdown-closer.js";
import { MarkdownHtmlTag } from "./markdown-html-tag.js";
import { MarkdownTextReferencesResources } from "./markdown-text-references.resources.js";

export class MarkdownHtmlScan {
private readonly text: string;
private readonly closers: Map<string, MarkdownCloser> = new Map();

private constructor(text: string) {
this.text = text;
}

public static tagsOf(text: string): readonly MarkdownHtmlTag[] {
const scan = new MarkdownHtmlScan(text);
const tags: MarkdownHtmlTag[] = [];
for (let at = text.indexOf(MarkdownTextReferencesResources.markdownHtmlTagStart); at >= 0;) {
const tag = scan.tagAt(at);
if (!Object.isNull(tag))
tags.push(tag);
at = text.indexOf(MarkdownTextReferencesResources.markdownHtmlTagStart, Object.isNull(tag) ? at + 1 : tag.end);
}
return tags;
}

private tagAt(at: number): MarkdownHtmlTag | null {
const pattern = MarkdownTextReferencesResources.markdownHtmlTagPattern;
pattern.lastIndex = at;
const tag = pattern.exec(this.text);
if (!Object.isNull(tag)) {
const name = tag[1]?.toLowerCase();
const opened = !Object.isUndefined(name) && tag[2] !== "/" && !MarkdownTextReferencesResources.markdownHtmlVoidTags.has(name) ? name : null;
return new MarkdownHtmlTag(at, pattern.lastIndex, opened, tag[3]?.toLowerCase() ?? null);
}
const span = MarkdownTextReferencesResources.markdownHtmlSpans.find(t => {
t.open.lastIndex = at;
return t.open.test(this.text);
});
if (Object.isUndefined(span))
return null;
const close = this.closerOf(span.close).at(span.open.lastIndex);
return close < 0 ? null : new MarkdownHtmlTag(at, close + span.close.length, null, null);
}

private closerOf(close: string): MarkdownCloser {
const known = this.closers.get(close);
if (!Object.isUndefined(known))
return known;
const closer = new MarkdownCloser(from => this.text.indexOf(close, from));
this.closers.set(close, closer);
return closer;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* @license
* Copyright (c) Noldova.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*/

export class MarkdownHtmlSpan {
public readonly open: RegExp;
public readonly close: string;

public constructor(open: RegExp, close: string) {
this.open = open;
this.close = close;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @license
* Copyright (c) Noldova.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*/

export class MarkdownHtmlTag {
public readonly start: number;
public readonly end: number;
public readonly opened: string | null;
public readonly closed: string | null;

public constructor(start: number, end: number, opened: string | null, closed: string | null) {
this.start = start;
this.end = end;
this.opened = opened;
this.closed = closed;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @license
* Copyright (c) Noldova.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*/

export class MarkdownMathDelimiter {
public readonly open: string;
public readonly close: string;
public readonly isDisplay: boolean;

public constructor(open: string, close: string, isDisplay: boolean) {
this.open = open;
this.close = close;
this.isDisplay = isDisplay;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* @license
* Copyright (c) Noldova.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*/

import type { StateInline } from "markdown-it";

import "@noldova/context.core";

import { Resources } from "../../resources.js";
import { MarkdownCloser } from "./markdown-closer.js";
import { MarkdownMathResources } from "./markdown-math.resources.js";

export class MarkdownMathScan {
private static readonly scans: WeakMap<StateInline, MarkdownMathScan> = new WeakMap();
private readonly source: string;
private readonly closers: Map<string, MarkdownCloser> = new Map();

private constructor(source: string) {
this.source = source;
}

public static of(state: StateInline): MarkdownMathScan {
const known = MarkdownMathScan.scans.get(state);
if (!Object.isUndefined(known))
return known;
const scan = new MarkdownMathScan(state.src);
MarkdownMathScan.scans.set(state, scan);
return scan;
}

public closingEnd(start: number, close: string): number {
const at = this.closerOf(close, from => this.source.indexOf(close, from)).at(start);
return at < 0 || String.isNullOrWhitespace(this.source.slice(start, at)) ? -1 : at + close.length;
}

public dollarEnd(start: number): number {
const isPaired = this.source.charAt(start) === MarkdownMathResources.markdownDollar || this.source.charAt(start - 2) === MarkdownMathResources.markdownDollar;
if (Resources.markdownWhitespacePattern.test(this.source.charAt(start)) || isPaired)
return -1;
const at = this.closerOf(MarkdownMathResources.markdownDollar, from => this.dollarAt(from)).at(start + 1);
return at < 0 ? -1 : at + 1;
}

private closerOf(key: string, find: (from: number) => number): MarkdownCloser {
const known = this.closers.get(key);
if (!Object.isUndefined(known))
return known;
const closer = new MarkdownCloser(find);
this.closers.set(key, closer);
return closer;
}

private dollarAt(from: number): number {
for (let at = this.source.indexOf(MarkdownMathResources.markdownDollar, from); at >= 0; at = this.source.indexOf(MarkdownMathResources.markdownDollar, at + 1)) {
const before = this.source.charAt(at - 1);
if (!Resources.markdownWhitespacePattern.test(before) && before !== MarkdownMathResources.markdownBackslash && !MarkdownMathResources.markdownDigitPattern.test(this.source.charAt(at + 1)))
return at;
}
return -1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,22 @@
* LICENSE file in the root directory of this source tree.
*/

import { MarkdownMathDelimiter } from "./markdown-math-delimiter.js";

export class MarkdownMathResources {
public static readonly markdownMathType: string = "math_inline";
public static readonly markdownMathDisplayType: string = "math_display";
public static readonly markdownMathBlockType: string = "math_block";
public static readonly markdownMathTag: string = "math";
public static readonly markdownMathDelimitersLongestFirst: readonly (readonly [string, string, boolean])[] = [["\\(", "\\)", false], ["\\[", "\\]", true], ["$$", "$$", true],
["$", "$", false]];
public static readonly markdownMathBlockDelimiters: readonly (readonly [string, string])[] = [["$$", "$$"], ["\\[", "\\]"]];
public static readonly markdownMathDelimitersLongestFirst: readonly MarkdownMathDelimiter[] = [new MarkdownMathDelimiter("\\(", "\\)", false),
new MarkdownMathDelimiter("\\[", "\\]", true), new MarkdownMathDelimiter("$$", "$$", true), new MarkdownMathDelimiter("$", "$", false)];
public static readonly markdownMathBlockDelimiters: readonly MarkdownMathDelimiter[] = [new MarkdownMathDelimiter("$$", "$$", true), new MarkdownMathDelimiter("\\[", "\\]", true)];
public static readonly markdownDollar: string = "$";
public static readonly markdownBackslash: string = "\\";
public static readonly markdownDigitPattern: RegExp = /\d/u;
public static readonly markdownLineParameter: string = "line";

public static formatMarkdownLineOutOfRange(line: number): string {
return `The block parser has no line ${line}.`;
}
}
51 changes: 22 additions & 29 deletions src/noldova.ui.components.angular/src/app/models/markdown-math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,27 @@
import type { StateBlock, StateInline } from "markdown-it";

import "@noldova/context.core";
import { ArgumentOutOfRangeException } from "@noldova/context.exceptions";

import { Resources } from "../../resources.js";
import { MarkdownMathScan } from "./markdown-math-scan.js";
import { MarkdownMathResources } from "./markdown-math.resources.js";

export class MarkdownMath {
public static inline(state: StateInline, silent: boolean): boolean {
if (state.env[Resources.markdownMathKey] === false)
return false;
const delimiters = MarkdownMathResources.markdownMathDelimitersLongestFirst.find(([open]) => state.src.startsWith(open, state.pos));
if (Object.isUndefined(delimiters))
const delimiter = MarkdownMathResources.markdownMathDelimitersLongestFirst.find(t => state.src.startsWith(t.open, state.pos));
if (Object.isUndefined(delimiter))
return false;
const [open, close, isDisplay] = delimiters;
const end = open === MarkdownMathResources.markdownDollar ? MarkdownMath.dollarEnd(state.src, state.pos + 1) : MarkdownMath.closingEnd(state.src, state.pos + open.length, close);
const scan = MarkdownMathScan.of(state);
const end = delimiter.open === MarkdownMathResources.markdownDollar ? scan.dollarEnd(state.pos + 1) : scan.closingEnd(state.pos + delimiter.open.length, delimiter.close);
if (end < 0)
return false;
if (!silent) {
const token = state.push(isDisplay ? MarkdownMathResources.markdownMathDisplayType : MarkdownMathResources.markdownMathType, MarkdownMathResources.markdownMathTag, 0);
token.markup = open;
token.content = state.src.slice(state.pos + open.length, end - close.length);
const token = state.push(delimiter.isDisplay ? MarkdownMathResources.markdownMathDisplayType : MarkdownMathResources.markdownMathType, MarkdownMathResources.markdownMathTag, 0);
token.markup = delimiter.open;
token.content = state.src.slice(state.pos + delimiter.open.length, end - delimiter.close.length);
token.info = state.src.slice(state.pos, end);
}
state.pos = end;
Expand All @@ -38,10 +40,10 @@ export class MarkdownMath {
if (state.env[Resources.markdownMathKey] === false)
return false;
const first = MarkdownMath.lineOf(state, startLine);
const delimiters = MarkdownMathResources.markdownMathBlockDelimiters.find(([open]) => first.startsWith(open));
if (Object.isUndefined(delimiters))
const delimiter = MarkdownMathResources.markdownMathBlockDelimiters.find(t => first.startsWith(t.open));
if (Object.isUndefined(delimiter))
return false;
const [open, close] = delimiters;
const { open, close } = delimiter;
const lines = [first];
for (let line = startLine; line < endLine; line++) {
const text = line === startLine ? first : MarkdownMath.lineOf(state, line);
Expand Down Expand Up @@ -71,28 +73,19 @@ export class MarkdownMath {
return false;
}

private static closingEnd(source: string, start: number, close: string): number {
const at = source.indexOf(close, start);
return at < 0 || String.isNullOrWhitespace(source.slice(start, at)) ? -1 : at + close.length;
}

private static dollarEnd(source: string, start: number): number {
const isPaired = source.charAt(start) === MarkdownMathResources.markdownDollar || source.charAt(start - 2) === MarkdownMathResources.markdownDollar;
if (Resources.markdownWhitespacePattern.test(source.charAt(start)) || isPaired)
return -1;
for (let at = source.indexOf(MarkdownMathResources.markdownDollar, start + 1); at >= 0; at = source.indexOf(MarkdownMathResources.markdownDollar, at + 1)) {
const before = source.charAt(at - 1);
if (!Resources.markdownWhitespacePattern.test(before) && before !== MarkdownMathResources.markdownBackslash && !MarkdownMathResources.markdownDigitPattern.test(source.charAt(at + 1)))
return at + 1;
}
return -1;
}

private static indentOf(state: StateBlock, line: number): number {
return state.sCount[line] as number;
return MarkdownMath.entryOf(state.sCount, line);
}

private static lineOf(state: StateBlock, line: number): string {
return state.src.slice((state.bMarks[line] as number) + (state.tShift[line] as number), state.eMarks[line]).trimEnd();
const start = MarkdownMath.entryOf(state.bMarks, line) + MarkdownMath.entryOf(state.tShift, line);
return state.src.slice(start, MarkdownMath.entryOf(state.eMarks, line)).trimEnd();
}

private static entryOf(entries: readonly number[], line: number): number {
const entry = entries[line];
if (Object.isUndefined(entry))
throw new ArgumentOutOfRangeException(MarkdownMathResources.markdownLineParameter, line, MarkdownMathResources.formatMarkdownLineOutOfRange(line));
return entry;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @license
* Copyright (c) Noldova.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*/

export class MarkdownTextPlace {
public readonly seen: number;
public readonly last: number;
public readonly line: number;

public constructor(seen: number, last: number, line: number) {
this.seen = seen;
this.last = last;
this.line = line;
}
}
Loading
Loading