mirror of
https://github.com/rabirabirara/obsidian-jelly-snippets.git
synced 2026-07-22 07:30:22 +00:00
feat: replace symbols works
This commit is contained in:
parent
65572b4632
commit
7c75776464
1 changed files with 49 additions and 11 deletions
60
symbol.ts
60
symbol.ts
|
|
@ -1,37 +1,75 @@
|
|||
import { RHS } from "snippet";
|
||||
|
||||
export enum Symbol {
|
||||
// on parse
|
||||
Newline = "\\nl\\",
|
||||
Tab = "\\tab\\",
|
||||
Newline = "%\\n",
|
||||
Tab = "%\\t",
|
||||
// on replace
|
||||
CursorEnd = "\\end\\",
|
||||
CursorEnd = "%\\e",
|
||||
}
|
||||
|
||||
export namespace Symbol {
|
||||
export const MAP: Record<string, string> = {
|
||||
const REPLACEABLE: Record<string, string> = {
|
||||
[Symbol.Newline]: "\n",
|
||||
[Symbol.Tab]: "\t",
|
||||
[Symbol.CursorEnd]: "",
|
||||
};
|
||||
export function replaceSymbols(inputStr: string): string {
|
||||
// const ON_REPLACE: Record<string, string> = {
|
||||
// [Symbol.CursorEnd]: "",
|
||||
// };
|
||||
export function replaceSymbolsOnParse(inputStr: string): RHS {
|
||||
const result: string[] = [];
|
||||
const len = inputStr.length;
|
||||
let i = 0;
|
||||
|
||||
let cursor = 0;
|
||||
let endFoundIdx = 0;
|
||||
let found;
|
||||
while (i < inputStr.length) {
|
||||
let found = false;
|
||||
for (const symbol in MAP) {
|
||||
found = false;
|
||||
for (const symbol in REPLACEABLE) {
|
||||
if (inputStr.startsWith(symbol, i)) {
|
||||
result.push(MAP[symbol]);
|
||||
result.push(REPLACEABLE[symbol]);
|
||||
console.log(REPLACEABLE[symbol]);
|
||||
cursor += REPLACEABLE[symbol].length;
|
||||
i += symbol.length;
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Check for cursorEnd symbol.
|
||||
if (inputStr.startsWith(Symbol.CursorEnd, i)) {
|
||||
i += Symbol.CursorEnd.length;
|
||||
// cursor marks the index in final string, we derive the "how far to move left" from it
|
||||
endFoundIdx = cursor;
|
||||
found = true;
|
||||
}
|
||||
if (!found) {
|
||||
result.push(inputStr[i]);
|
||||
cursor++;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
let data = result.join("");
|
||||
let info = {
|
||||
cursorEnd: data.length - endFoundIdx,
|
||||
};
|
||||
|
||||
return result.join("");
|
||||
return { data, info };
|
||||
}
|
||||
}
|
||||
|
||||
// Some tests!
|
||||
// let rhs1 = Symbol.replaceSymbolsOnParse("123%\\e45678");
|
||||
// console.log(rhs1);
|
||||
// let rhs2 = Symbol.replaceSymbolsOnParse("12345678%\\e");
|
||||
// console.log(rhs2);
|
||||
// let rhs3 = Symbol.replaceSymbolsOnParse("%\\e12345678");
|
||||
// console.log(rhs3);
|
||||
// let rhs4 = Symbol.replaceSymbolsOnParse("12%\\e3");
|
||||
// console.log(rhs4);
|
||||
// let rhs5 = Symbol.replaceSymbolsOnParse("1%\\e23");
|
||||
// console.log(rhs5);
|
||||
// let rhs6 = Symbol.replaceSymbolsOnParse("%\\e123");
|
||||
// console.log(rhs6);
|
||||
// let rhs7 = Symbol.replaceSymbolsOnParse("123%\\n4%\\e56");
|
||||
// console.log(rhs7);
|
||||
// console.log(rhs7.data);
|
||||
|
|
|
|||
Loading…
Reference in a new issue