Skip to content

Commit

Permalink
Merge pull request #98 from st3v3nmw/develop
Browse files Browse the repository at this point in the history
Ignore codeblocks
  • Loading branch information
st3v3nmw authored May 21, 2021
2 parents 8b84184 + 390aa72 commit 547946b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const WIKILINK_MEDIA_REGEX: RegExp =
export const MARKDOWN_LINK_MEDIA_REGEX: RegExp =
/!\[.*\]\((.*.(?:png|jpe?g|gif|bmp|svg))\)/gm; // ![...](...) format

export const CODEBLOCK_REGEX: RegExp = /```(?:.*\n)*```/gm;
export const CODEBLOCK_REGEX: RegExp = /```(?:.*\n)*?```/gm;
export const INLINE_CODE_REGEX: RegExp = /`(?!`).+`/gm;

export const CROSS_HAIRS_ICON: string = `<path style=" stroke:none;fill-rule:nonzero;fill:currentColor;fill-opacity:1;" d="M 99.921875 47.941406 L 93.074219 47.941406 C 92.84375 42.03125 91.390625 36.238281 88.800781 30.921875 L 85.367188 32.582031 C 87.667969 37.355469 88.964844 42.550781 89.183594 47.84375 L 82.238281 47.84375 C 82.097656 44.617188 81.589844 41.417969 80.734375 38.304688 L 77.050781 39.335938 C 77.808594 42.089844 78.261719 44.917969 78.40625 47.769531 L 65.871094 47.769531 C 64.914062 40.507812 59.144531 34.832031 51.871094 33.996094 L 51.871094 21.386719 C 54.816406 21.507812 57.742188 21.960938 60.585938 22.738281 L 61.617188 19.058594 C 58.4375 18.191406 55.164062 17.691406 51.871094 17.570312 L 51.871094 10.550781 C 57.164062 10.769531 62.355469 12.066406 67.132812 14.363281 L 68.789062 10.929688 C 63.5 8.382812 57.738281 6.953125 51.871094 6.734375 L 51.871094 0.0390625 L 48.054688 0.0390625 L 48.054688 6.734375 C 42.179688 6.976562 36.417969 8.433594 31.132812 11.007812 L 32.792969 14.441406 C 37.566406 12.140625 42.761719 10.84375 48.054688 10.625 L 48.054688 17.570312 C 44.828125 17.714844 41.628906 18.21875 38.515625 19.078125 L 39.546875 22.757812 C 42.324219 21.988281 45.175781 21.53125 48.054688 21.386719 L 48.054688 34.03125 C 40.796875 34.949219 35.089844 40.679688 34.203125 47.941406 L 21.5 47.941406 C 21.632812 45.042969 22.089844 42.171875 22.855469 39.375 L 19.171875 38.34375 C 18.3125 41.457031 17.808594 44.65625 17.664062 47.882812 L 10.664062 47.882812 C 10.882812 42.589844 12.179688 37.394531 14.480469 32.621094 L 11.121094 30.921875 C 8.535156 36.238281 7.078125 42.03125 6.847656 47.941406 L 0 47.941406 L 0 51.753906 L 6.847656 51.753906 C 7.089844 57.636719 8.542969 63.402344 11.121094 68.695312 L 14.554688 67.035156 C 12.257812 62.261719 10.957031 57.066406 10.738281 51.773438 L 17.742188 51.773438 C 17.855469 55.042969 18.34375 58.289062 19.191406 61.445312 L 22.871094 60.414062 C 22.089844 57.5625 21.628906 54.632812 21.5 51.679688 L 34.203125 51.679688 C 35.058594 58.96875 40.773438 64.738281 48.054688 65.660156 L 48.054688 78.308594 C 45.105469 78.1875 42.183594 77.730469 39.335938 76.957031 L 38.304688 80.636719 C 41.488281 81.511719 44.757812 82.015625 48.054688 82.144531 L 48.054688 89.144531 C 42.761719 88.925781 37.566406 87.628906 32.792969 85.328125 L 31.132812 88.765625 C 36.425781 91.3125 42.183594 92.742188 48.054688 92.960938 L 48.054688 99.960938 L 51.871094 99.960938 L 51.871094 92.960938 C 57.75 92.71875 63.519531 91.265625 68.808594 88.6875 L 67.132812 85.253906 C 62.355469 87.550781 57.164062 88.851562 51.871094 89.070312 L 51.871094 82.125 C 55.09375 81.980469 58.292969 81.476562 61.40625 80.617188 L 60.378906 76.9375 C 57.574219 77.703125 54.695312 78.15625 51.792969 78.289062 L 51.792969 65.679688 C 59.121094 64.828125 64.910156 59.0625 65.796875 51.734375 L 78.367188 51.734375 C 78.25 54.734375 77.789062 57.710938 76.992188 60.605469 L 80.675781 61.636719 C 81.558594 58.40625 82.066406 55.082031 82.183594 51.734375 L 89.261719 51.734375 C 89.042969 57.03125 87.742188 62.222656 85.445312 66.996094 L 88.878906 68.65625 C 91.457031 63.367188 92.910156 57.597656 93.152344 51.71875 L 100 51.71875 Z M 62.019531 51.734375 C 61.183594 56.945312 57.085938 61.023438 51.871094 61.828125 L 51.871094 57.515625 L 48.054688 57.515625 L 48.054688 61.808594 C 42.910156 60.949219 38.886719 56.902344 38.058594 51.753906 L 42.332031 51.753906 L 42.332031 47.941406 L 38.058594 47.941406 C 38.886719 42.789062 42.910156 38.746094 48.054688 37.886719 L 48.054688 42.179688 L 51.871094 42.179688 L 51.871094 37.847656 C 57.078125 38.648438 61.179688 42.71875 62.019531 47.921875 L 57.707031 47.921875 L 57.707031 51.734375 Z M 62.019531 51.734375 "/>`;
Expand Down
51 changes: 45 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import {
CLOZE_SCHEDULING_EXTRACTOR,
WIKILINK_MEDIA_REGEX,
MARKDOWN_LINK_MEDIA_REGEX,
CODEBLOCK_REGEX,
INLINE_CODE_REGEX,
} from "./constants";
import { escapeRegexString } from "./utils";

Expand Down Expand Up @@ -508,6 +510,13 @@ export default class SRPlugin extends Plugin {
this.newFlashcards[deck] = [];
}

// find all codeblocks
let codeblocks: [number, number][] = [];
for (let regex of [CODEBLOCK_REGEX, INLINE_CODE_REGEX]) {
for (let match of fileText.matchAll(regex))
codeblocks.push([match.index, match.index + match[0].length]);
}

let now = Date.now();
// basic cards
for (let regex of [this.singlelineCardRegex, this.multilineCardRegex]) {
Expand All @@ -516,15 +525,20 @@ export default class SRPlugin extends Plugin {
? CardType.SingleLineBasic
: CardType.MultiLineBasic;
for (let match of fileText.matchAll(regex)) {
if (
inCodeblock(match.index, match[0].trim().length, codeblocks)
)
continue;

let cardText = match[0].trim();
let originalFrontText = match[1].trim();
let front = await this.fixCardMediaLinks(
match[1].trim(),
originalFrontText,
note.path
);
let originalBackText = match[2].trim();
let back = await this.fixCardMediaLinks(
match[2].trim(),
originalBackText,
note.path
);
let cardObj: Card;
Expand Down Expand Up @@ -581,10 +595,20 @@ export default class SRPlugin extends Plugin {
match[0] = match[0].trim();

let cardText = match[0];
let deletions = [
...cardText.matchAll(CLOZE_DELETIONS_EXTRACTOR),
];
let scheduling = [

let deletions: RegExpMatchArray[] = [];
for (let m of cardText.matchAll(CLOZE_DELETIONS_EXTRACTOR)) {
if (
inCodeblock(
match.index + m.index,
m[0].trim().length,
codeblocks
)
)
continue;
deletions.push(m);
}
let scheduling: RegExpMatchArray[] = [
...cardText.matchAll(CLOZE_SCHEDULING_EXTRACTOR),
];

Expand Down Expand Up @@ -747,3 +771,18 @@ function addContextToCard(
for (let headingObj of stack) cardObj.context += headingObj.heading + " > ";
cardObj.context = cardObj.context.slice(0, -3);
}

function inCodeblock(
matchStart: number,
matchLength: number,
codeblocks: [number, number][]
) {
for (let codeblock of codeblocks) {
if (
matchStart >= codeblock[0] &&
matchStart + matchLength <= codeblock[1]
)
return true;
}
return false;
}

0 comments on commit 547946b

Please sign in to comment.