Skip to content

Commit

Permalink
Addition of markdownID to class
Browse files Browse the repository at this point in the history
  • Loading branch information
flengyel committed Jul 3, 2023
1 parent 82d35ce commit 4e6ae6f
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions src/AsyncZettelViewTreeItem.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

import * as vscode from 'vscode';
import * as fs from 'fs';
import * as path from 'path';
Expand All @@ -8,6 +9,7 @@ import { IncomingLinksMap } from './utils/IncomingLinksMap';
export class AsyncZettelViewTreeItem extends vscode.TreeItem {
public incomingLinks: Set<string> = new Set<string>();
private idMatch = false;
private markdownID: string;

constructor(
public readonly pathname: string,
Expand All @@ -18,58 +20,59 @@ export class AsyncZettelViewTreeItem extends vscode.TreeItem {
) {
super(basename, collapsibleState);
this.label = basename;
const ID = extractIDFromFilename(basename);
this.markdownID = extractIDFromFilename(basename);

// The rest of your code...

// since we cannot make an asynchronous call to a constructor
// and we want to consume a stream line-by-line, we return
// an IIAFE: immediately invoked Asynchronous Function Expression
// See: https://stackoverflow.com/questions/43431550/async-await-class-constructor/50885340#50885340

return (async (): Promise<AsyncZettelViewTreeItem> =>{
return (async (): Promise<AsyncZettelViewTreeItem> =>{
try {
const fileStream = fs.createReadStream(pathname);
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity,
crlfDelay: Infinity,
});
// Read the file line by line

// Duplication to remove the error that "this.markdownID is used before it is assigned"
this.markdownID = extractIDFromFilename(basename);

for await (const line of rl) {
const h1match = idregex.h1RegExp.exec(line);
if (h1match) {
// check if basename == match[1].md
if (basename !== `${h1match[1]}.md`) {
logger(`ID ${h1match[1]} does not match filename ${basename}`);
// vscode.window.showInformationMessage(`ID ${match[1]} does not match filename ${basename}`);
} else {
this.idMatch = true;
// show the H1 header only when the ID matches the filename
this.label = line;
}
}
// Regex to match markdown links, capture the ID
// all links are of the form [[ID]] or [[ID|TITLE]]
// I don't know if Zettlr understands [[ID|TITLE]]
let match;
const linkRegex = /\[\[([\w\\.]+)\]\]/g;
while ((match = linkRegex.exec(line)) !== null) {
this.incomingLinksMap.addLink(match[1], ID); // Use the extracted ID here
this.incomingLinksMap.addLink(match[1], this.markdownID);
}


}

if (!this.idMatch) {
logger(`# ID TITLE not found in: ${basename}`);
}
// vscode.window.showInformationMessage(`# ID TITLE header not found in: ${basename}`);

rl.close();
this.incomingLinks = this.incomingLinksMap.getIncomingLinksFor(this.markdownID);

// Add the set of incoming links for this file to this object
this.incomingLinks = this.incomingLinksMap.getIncomingLinksFor(ID);
return this;
} catch (err) {
console.error(err);
return this;
}
})() as unknown as AsyncZettelViewTreeItem;
}

getID(): string {
return this.markdownID;
}
}

0 comments on commit 4e6ae6f

Please sign in to comment.