diff options
author | crupest <crupest@outlook.com> | 2022-04-19 17:20:20 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2022-04-19 17:20:20 +0800 |
commit | b9c770e63da37f3a33e46c69a09f21b4b8d6ad8b (patch) | |
tree | 9093585585808a35ed042677be8f5dbb8df5c1e4 /FrontEnd/src/services/TimelinePostBuilder.ts | |
parent | 00d61732aecbefa89b58d9dbc4c125713ddad7eb (diff) | |
download | timeline-b9c770e63da37f3a33e46c69a09f21b4b8d6ad8b.tar.gz timeline-b9c770e63da37f3a33e46c69a09f21b4b8d6ad8b.tar.bz2 timeline-b9c770e63da37f3a33e46c69a09f21b4b8d6ad8b.zip |
...
Diffstat (limited to 'FrontEnd/src/services/TimelinePostBuilder.ts')
-rw-r--r-- | FrontEnd/src/services/TimelinePostBuilder.ts | 36 |
1 files changed, 22 insertions, 14 deletions
diff --git a/FrontEnd/src/services/TimelinePostBuilder.ts b/FrontEnd/src/services/TimelinePostBuilder.ts index 40279eca..0c21b353 100644 --- a/FrontEnd/src/services/TimelinePostBuilder.ts +++ b/FrontEnd/src/services/TimelinePostBuilder.ts @@ -1,29 +1,37 @@ -import { Remarkable } from "remarkable"; +import { marked } from "marked"; import { UiLogicError } from "@/common"; import { base64 } from "@/http/common"; import { HttpTimelinePostPostRequest } from "@/http/timeline"; +class TimelinePostMarkedRenderer extends marked.Renderer { + constructor(private _images: { file: File; url: string }[]) { + super(); + } + + image(href: string | null, title: string | null, text: string): string { + if (href != null) { + const i = parseInt(href); + if (!isNaN(i) && i > 0 && i <= this._images.length) { + href = this._images[i - 1].url; + } + } + return this.image(href, title, text); + } +} + export default class TimelinePostBuilder { private _onChange: () => void; private _text = ""; private _images: { file: File; url: string }[] = []; - private _md: Remarkable = new Remarkable(); + private _markedOptions: marked.MarkedOptions; constructor(onChange: () => void) { this._onChange = onChange; - const oldImageRenderer = this._md.renderer.rules.image; - this._md.renderer.rules.image = (( - _t: TimelinePostBuilder - ): Remarkable.Rule<Remarkable.ImageToken, string> => - function (tokens, idx, options /*, env */) { - const i = parseInt(tokens[idx].src); - if (!isNaN(i) && i > 0 && i <= _t._images.length) { - tokens[idx].src = _t._images[i - 1].url; - } - return oldImageRenderer(tokens, idx, options); - })(this); + this._markedOptions = { + renderer: new TimelinePostMarkedRenderer(this._images), + }; } setMarkdownText(text: string): void { @@ -87,7 +95,7 @@ export default class TimelinePostBuilder { } renderHtml(): string { - return this._md.render(this._text); + return marked.parse(this._text); } dispose(): void { |