mirror of
https://github.com/thelounge/thelounge
synced 2024-11-21 19:43:07 +00:00
Move md-parsing logic out of ParsedMessage
This commit is contained in:
parent
34428da5c7
commit
f01a90fb71
2 changed files with 40 additions and 36 deletions
|
@ -1,15 +1,9 @@
|
|||
<script lang="ts">
|
||||
import {defineComponent, PropType} from "vue";
|
||||
import parse from "../js/helpers/parse";
|
||||
import {
|
||||
ParseFragment,
|
||||
createPlaceholder,
|
||||
rehydrate,
|
||||
parseMd,
|
||||
idGenerator,
|
||||
} from "../js/helpers/mdHelpers";
|
||||
import type {ClientMessage, ClientNetwork} from "../js/types";
|
||||
import {useStore} from "../js/store";
|
||||
import {parseMarkdownMessage} from "../js/helpers/mdHelpers";
|
||||
|
||||
export default defineComponent({
|
||||
name: "ParsedMessage",
|
||||
|
@ -34,32 +28,7 @@ export default defineComponent({
|
|||
);
|
||||
|
||||
if (context?.message?.type === "message" && this.store.state.settings.parseMd) {
|
||||
const htmls: Map<number, ParseFragment> = new Map();
|
||||
const id = idGenerator();
|
||||
|
||||
const generateStandIns = (nodes) => {
|
||||
const result: string[] = [];
|
||||
|
||||
for (let i = 0; i < nodes.length; i++) {
|
||||
if (nodes[i] instanceof Array) {
|
||||
result.push(...generateStandIns(nodes[i]));
|
||||
} else {
|
||||
if (typeof nodes[i] === "string") {
|
||||
result.push(nodes[i]);
|
||||
} else {
|
||||
const nextId = id();
|
||||
|
||||
htmls.set(nextId, nodes[i]);
|
||||
result.push(createPlaceholder(nextId));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
const toParse = "".concat(...generateStandIns(parsed));
|
||||
return rehydrate(parseMd(toParse, this.store.state.settings.renderMdSrc), htmls);
|
||||
return parseMarkdownMessage(parsed, this.store.state.settings.renderMdSrc);
|
||||
}
|
||||
|
||||
return parsed;
|
||||
|
|
|
@ -28,7 +28,7 @@ export type ParseFragment =
|
|||
| undefined
|
||||
)[];
|
||||
|
||||
export const parseMd = (src: string, renderMdSrc) => {
|
||||
const parseMd = (src: string, renderMdSrc) => {
|
||||
let i = 0;
|
||||
const result: string[] = [];
|
||||
|
||||
|
@ -161,7 +161,7 @@ function mapDOM(element: HTMLElement | string): DomElementRepr {
|
|||
return treeObject as DomElementRepr;
|
||||
}
|
||||
|
||||
export const rehydrate = (parsed: string, htmls: Map<number, ParseFragment>) => {
|
||||
const rehydrate = (parsed: string, htmls: Map<number, ParseFragment>) => {
|
||||
const parsedDom = mapDOM(parsed);
|
||||
const result: (ParseFragment | string)[] = [];
|
||||
|
||||
|
@ -208,7 +208,7 @@ export const rehydrate = (parsed: string, htmls: Map<number, ParseFragment>) =>
|
|||
return result;
|
||||
};
|
||||
|
||||
export const idGenerator = () => {
|
||||
const idGenerator = () => {
|
||||
const ids = new Set();
|
||||
|
||||
const genId = () => {
|
||||
|
@ -223,3 +223,38 @@ export const idGenerator = () => {
|
|||
|
||||
return genId;
|
||||
};
|
||||
|
||||
export const parseMarkdownMessage = (
|
||||
message: (
|
||||
| VNode<RendererNode, RendererElement, {[key: string]: any}>
|
||||
| (string | VNode<RendererNode, RendererElement, {[key: string]: any}> | undefined)[]
|
||||
)[],
|
||||
renderMdSrc: boolean
|
||||
): ParseFragment[] => {
|
||||
const htmls: Map<number, ParseFragment> = new Map();
|
||||
const id = idGenerator();
|
||||
|
||||
const generateStandIns = (nodes): string[] => {
|
||||
const result: string[] = [];
|
||||
|
||||
for (let i = 0; i < nodes.length; i++) {
|
||||
if (nodes[i] instanceof Array) {
|
||||
result.push(...generateStandIns(nodes[i]));
|
||||
} else {
|
||||
if (typeof nodes[i] === "string") {
|
||||
result.push(nodes[i]);
|
||||
} else {
|
||||
const nextId = id();
|
||||
|
||||
htmls.set(nextId, nodes[i]);
|
||||
result.push(createPlaceholder(nextId));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
const toParse = "".concat(...generateStandIns(message));
|
||||
return rehydrate(parseMd(toParse, renderMdSrc), htmls);
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue