svelte render: simplify replaces
This commit is contained in:
parent
05069b15d8
commit
bd2b7e7579
@ -104,42 +104,20 @@ export default class SvelteViewEngine extends ViewEngine {
|
||||
const props = JSON.stringify(localMap);
|
||||
|
||||
// Replaces
|
||||
const replaces: { [key: string]: string } = {
|
||||
const replaceMap: Record<string, string> = {
|
||||
canonicalViewName: canonicalViewName,
|
||||
props: props,
|
||||
head: head,
|
||||
html: html,
|
||||
css: css,
|
||||
};
|
||||
const replaceOperations: Record<number, {
|
||||
key: string;
|
||||
replaceValue: string;
|
||||
} | undefined> = {};
|
||||
for (const entry of Object.entries(replaces)) {
|
||||
const matches = rawOutput.matchAll(new RegExp(`%${entry[0]}%`, 'g'));
|
||||
for (const match of matches) {
|
||||
if (typeof match.index === 'number') {
|
||||
replaceOperations[match.index] = {
|
||||
key: `%${entry[0]}%`,
|
||||
replaceValue: entry[1],
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
let output = '';
|
||||
for (let i = 0; i < rawOutput.length; i++) {
|
||||
const replaceOperation = replaceOperations[i];
|
||||
if (replaceOperation) {
|
||||
output += replaceOperation.replaceValue;
|
||||
i += replaceOperation.key.length - 1;
|
||||
} else {
|
||||
output += rawOutput[i];
|
||||
}
|
||||
}
|
||||
|
||||
return output;
|
||||
return rawOutput.replace(
|
||||
new RegExp(Object.keys(replaceMap).map(str => `%${str}%`).join('|'), 'g'),
|
||||
(substring) => {
|
||||
console.log(substring);
|
||||
return replaceMap[substring.slice(1, substring.length - 1)];
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
public async stop(): Promise<void> {
|
||||
|
Loading…
Reference in New Issue
Block a user