Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 219 correctly handle dolar symbol in decensor function #618

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions src/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,28 @@ export class FormatConverter {
return [note_text.replace(regexp, mask), matches]
}

decensor(note_text: string, mask:string, replacements: string[], escape: boolean): string {
for (let replacement of replacements) {
note_text = note_text.replace(
mask, escape ? escapeHtml(replacement) : replacement
)
decensor(note_text: string, mask: string, replacements: string[], escape: boolean): string {
let index = 0;

// note_text example: "The OBSTOANKICODEDISPLAY is worth OBSTOANKICODEDISPLAY today"
// maskGlobalReg example: /OBSTOANKICODEDISPLAY/g
const maskGlobalRegex: RegExp = new RegExp(mask, 'g');

const matchCount: number = (note_text.match(maskGlobalRegex) || []).length;

// Validate that we have exactly enough replacements
if (matchCount !== replacements.length) {
throw new Error(`Mismatch between placeholders (${matchCount}) and replacements (${replacements.length})`);
}
return note_text

// replacements example: ["10", "15"]
note_text = note_text.replace(maskGlobalRegex, () => {
const replacement: string = replacements[index++];
return escape ? escapeHtml(replacement) : replacement;
});

// note_text expected: "The 10 is worth 15 today"
return note_text;
}

format(note_text: string, cloze: boolean, highlights_to_cloze: boolean): string {
Expand Down