Skip to content

Commit

Permalink
Inline regex
Browse files Browse the repository at this point in the history
  • Loading branch information
mgmeyers committed Jan 1, 2024
1 parent 04f3e8a commit e528794
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 14 deletions.
7 changes: 2 additions & 5 deletions src/bbt/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,11 @@ export async function mkMDDir(mdPath: string) {
await app.vault.createFolder(dir);
}

const toSpaceRegEx = /\s*[*?]+\s*/g;
const toDashRegEx = /\s*[:"<>|]+\s*/g;

export function replaceIllegalChars(str: string) {
return str
.replace(toSpaceRegEx, ' ')
.replace(/\s*[*?]+\s*/g, ' ')
.trim()
.replace(toDashRegEx, ' - ')
.replace(/\s*[:"<>|]+\s*/g, ' - ')
.trim();
}

Expand Down
14 changes: 5 additions & 9 deletions src/bbt/template.env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,18 +182,16 @@ export class PersistExtension implements Extension {
);
}

static re = /%% begin (.+?) %%([\w\W]*?)%% end \1 %%/gi;
static hasPersist(str: string) {
this.re.lastIndex = 0;
return this.re.test(str);
return /%% begin (.+?) %%([\w\W]*?)%% end \1 %%/gi.test(str);
}
static prepareTemplateData<T>(templateData: T, md: string): T & WithRetained {
const out: Record<string, string> = {};

if (!md) return templateData;

this.re.lastIndex = 0;
for (const match of md.matchAll(this.re)) {
const matches = md.matchAll(/%% begin (.+?) %%([\w\W]*?)%% end \1 %%/gi);
for (const match of matches) {
out[match[1]] = match[2];
}

Expand All @@ -208,21 +206,19 @@ export class ObsidianMarkdownLoader extends Loader {
sourceFile: string;

async = true;
wikiLinkRe = /^\[\[([^\]]+)\]\]$/;
markdownLinkRe = /^\[[^\]]*\]\(([^)]+)\)$/;

setSourceFile(path: string) {
this.sourceFile = path;
}

getLinkPath(link: string) {
let match = link.trim().match(this.wikiLinkRe);
let match = link.trim().match(/^\[\[([^\]]+)\]\]$/);

if (match) {
return match[1];
}

match = link.trim().match(this.markdownLinkRe);
match = link.trim().match(/^\[[^\]]*\]\(([^)]+)\)$/);

if (match) {
return match[1];
Expand Down

0 comments on commit e528794

Please sign in to comment.