Improve markdown tokenizer

This commit is contained in:
Igor Kulikov 2021-10-22 13:27:55 +03:00
parent cb75f8a430
commit 07dca7ef63

View File

@ -19,6 +19,8 @@ import { Inject, Injectable } from '@angular/core';
import { TranslateService } from '@ngx-translate/core'; import { TranslateService } from '@ngx-translate/core';
import { DOCUMENT } from '@angular/common'; import { DOCUMENT } from '@angular/common';
import { WINDOW } from '@core/services/window.service'; import { WINDOW } from '@core/services/window.service';
import { Tokenizer } from 'marked';
import * as marked from 'marked';
const copyCodeBlock = '{:copy-code}'; const copyCodeBlock = '{:copy-code}';
const autoBlock = '{:auto}'; const autoBlock = '{:auto}';
@ -37,6 +39,7 @@ export class MarkedOptionsService extends MarkedOptions {
pedantic = false; pedantic = false;
smartLists = true; smartLists = true;
smartypants = false; smartypants = false;
mangle = false;
private renderer2 = new MarkedRenderer(); private renderer2 = new MarkedRenderer();
@ -46,6 +49,26 @@ export class MarkedOptionsService extends MarkedOptions {
@Inject(WINDOW) private readonly window: Window, @Inject(WINDOW) private readonly window: Window,
@Inject(DOCUMENT) private readonly document: Document) { @Inject(DOCUMENT) private readonly document: Document) {
super(); super();
// @ts-ignore
const tokenizer: Tokenizer = {
autolink(src: string, mangle: (cap: string) => string): marked.Tokens.Link {
if (src.endsWith(copyCodeBlock)) {
return undefined;
} else {
// @ts-ignore
return false;
}
},
url(src: string, mangle: (cap: string) => string): marked.Tokens.Link {
if (src.endsWith(copyCodeBlock)) {
return undefined;
} else {
// @ts-ignore
return false;
}
}
};
marked.use({tokenizer});
this.renderer.code = (code: string, language: string | undefined, isEscaped: boolean) => { this.renderer.code = (code: string, language: string | undefined, isEscaped: boolean) => {
if (code.endsWith(copyCodeBlock)) { if (code.endsWith(copyCodeBlock)) {
code = code.substring(0, code.length - copyCodeBlock.length); code = code.substring(0, code.length - copyCodeBlock.length);