Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion esbuild.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ esbuild
platform: 'node',
format: 'cjs',
watch: !prod,
target: 'es2016',
target: 'es2021',
logLevel: 'info',
loader: {
'.pegjs': 'text',
Expand Down
1 change: 1 addition & 0 deletions src/blueprints/basic-codeblock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export class BasicCodeBlockBlueprint extends CodeBlockBlueprint {
delete: note.config.delete,
enabled: note.config.enabled,
cloze: note.config.cloze,
clozeReplacements: note.config.clozeReplacements,
})

let str = ''
Expand Down
1 change: 1 addition & 0 deletions src/blueprints/sandwich.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export class SandwichBlueprint extends Blueprint {
delete: note.config.delete,
enabled: note.config.enabled,
cloze: note.config.cloze,
clozeReplacements: note.config.clozeReplacements,
})

let str = ''
Expand Down
2 changes: 2 additions & 0 deletions src/notes/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface Config {
delete?: boolean
enabled?: boolean
cloze?: boolean
clozeReplacements?: Array<string>
}

export interface ParseConfig extends Config {
Expand All @@ -42,6 +43,7 @@ export const ParseConfigSchema: yup.SchemaOf<ParseConfig> = yup.object({
delete: yup.boolean().nullAsUndefined(),
enabled: yup.boolean().nullAsUndefined(),
cloze: yup.boolean().nullAsUndefined(),
clozeReplacements: yup.array().of(yup.string()).notRequired(),
})

// Location
Expand Down
46 changes: 46 additions & 0 deletions src/processors/postprocessors/cloze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,35 @@ import { NoteBase } from 'ankibridge/notes/base'
import { ProcessorContext } from 'ankibridge/processors/base'
import { Postprocessor } from 'ankibridge/processors/postprocessors/base'

const processClozeReplacement = (raw: string): [RegExp | string, string] => {
// regex, e.g.: /foobar/g
const regexModeMatching = raw.match(/^\/(.*?)\/(.?)$/)
if (regexModeMatching) {
const pattern = regexModeMatching[1]
const flags = regexModeMatching[2]
const replaceValue = "$1"
return [new RegExp(`(${pattern})`, flags), replaceValue]
}

// regex with specified replace value, e.g.: /foo(bar)/g, $1
const regexWithReplaceValueModeMatching = raw.match(/^\/(.*?)\/(.?),\s+(.*?)$/)
if (regexWithReplaceValueModeMatching) {
const pattern = regexWithReplaceValueModeMatching[1]
const flags = regexWithReplaceValueModeMatching[2]
const replaceValue = regexWithReplaceValueModeMatching[3]
return [new RegExp(pattern, flags), replaceValue]
}

// raw text, e.g.: r"/123/"
const textModeMatching = raw.match(/^r"(.*?)"$/)
if (textModeMatching) {
return [textModeMatching[1], textModeMatching[1]]
}

// text, e.g.: 123
return [raw, raw]
}

export class ClozePostprocessor extends Postprocessor {
static id = 'ClozePostprocessor'
static displayName = 'ClozePostprocessor'
Expand All @@ -29,16 +58,33 @@ export class ClozePostprocessor extends Postprocessor {
targets = targets.concat(Array.from(domField.content.querySelectorAll('del')))
}

console.log("domField", domField)

targets.forEach((target) => {
const content = target.textContent
const cloze = `{{c${clozeIterator}::${content}}}`
const clozeNode = document.createTextNode(cloze)

console.log("cloze", clozeNode)
console.log("target", target)

target.replaceWith(clozeNode)

clozeIterator++
})

if (note.config.clozeReplacements) {
const clozeReplacements = note.config.clozeReplacements
for (const replacement of clozeReplacements) {
const [searchValue, replaceValue] = processClozeReplacement(replacement)

console.log("tuple", searchValue, replaceValue)
console.log("domField2", domField)
domField.innerHTML = domField.innerHTML.replaceAll(searchValue, `{{c${clozeIterator}::${replaceValue}}}`)
clozeIterator++
}
}

if (clozeIterator !== 1) {
note.isCloze = true
}
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"es2015",
"es2019",
"es2020",
"es2021"
],
"types": [
"node",
Expand Down