Skip to content

Commit

Permalink
Maintain a list of IDs each markdown file links to
Browse files Browse the repository at this point in the history
  • Loading branch information
flengyel committed Jun 26, 2023
1 parent f49493f commit 2b5887d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 28 deletions.
62 changes: 35 additions & 27 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import { replaceLinks } from './replaceLinks';
class AsyncZettelViewTreeItem extends vscode.TreeItem {
//public label: string; // label is public in TreeItem!
// no wonder this works...

// retain the list of WikiLinks in the Zettel
public linkedIDs: Set<string> = new Set<string>();
private idMatch = false;
constructor(
public readonly pathname: string,
public readonly basename: string,
Expand All @@ -36,30 +38,35 @@ class AsyncZettelViewTreeItem extends vscode.TreeItem {
input: fileStream,
crlfDelay: Infinity,
});

// The mixed technique does not work--
// rl.on('line', (line) =>


// Read the file line by line
for await (const line of rl) {

//const match = line?.match(/^# ((\w{1,4}\.){2,}\d\w{3})/);
// id is non-local
const match = id.h1re.exec(line);
if (match) {
const h1match = id.h1re.exec(line);
if (h1match) {
// check if basename == match[1].md
if (basename !== `${match[1]}.md`) {
myLogger.logMsg(`ID ${match[1]} does not match filename ${basename}`);
if (basename !== `${h1match[1]}.md`) {
myLogger.logMsg(`ID ${h1match[1]} does not match filename ${basename}`);
// vscode.window.showInformationMessage(`ID ${match[1]} does not match filename ${basename}`);
} else {
this.idMatch = true;
this.label = line; // show the H1 header only when the ID matches the filename
// That way you know you have to update the filename or the ID
}
rl.close(); // we're done
return this;
}
// Regex to match markdown links, capture the ID
let match;
const linkRegex = /\[\[([\w\\.]+)\]\]/g;
while ((match = linkRegex.exec(line)) !== null) {
// match[1] is the ID
// vscode.window.showInformationMessage(`Found ID: ${match[1]}`);
if (match[1] !== this.basename) {
this.linkedIDs.add(match[1]);
}
}
}
if (!this.idMatch) {
myLogger.logMsg(`# ID TITLE not found in: ${basename}`);
}

myLogger.logMsg(`# ID TITLE not found in: ${basename}`);
// vscode.window.showInformationMessage(`# ID TITLE header not found in: ${basename}`);
rl.close();
return this;
Expand Down Expand Up @@ -160,24 +167,25 @@ export function activate(context: vscode.ExtensionContext): void {
return;
}

try {
// Assume node.fsPath is the file path of the file to be renamed
const oldPath = node.fsPath;
//concatenate the new ID with the extension ".md"
const newName = `${newID}.md`;
const newPath = path.join(path.dirname(oldPath), newName);

// Assume node.fsPath is the file path of the file to be renamed
const oldPath = node.fsPath;
//concatenate the new ID with the extension ".md"
const newName = `${newID}.md`;
const newPath = path.join(path.dirname(oldPath), newName);

try {
// Rename the file
await fs.promises.rename(oldPath, newPath);

// Now find and replace all the links in the workspace
await replaceLinks(oldPath, newPath);

// Refresh the tree view
provider.refresh();
} catch (error) {
myLogger.logMsg(`Failed to rename file: ${error}`);
}
// Now find and replace all the links in the workspace
await replaceLinks(oldPath, newPath);

// Refresh the tree view
provider.refresh();
}
});
}
Expand Down
7 changes: 6 additions & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export class IDregex {
private _h1regex: string; // save the pattern as a string
private _idre: RegExp; // save the compiled regex
private _idregex: string; // save the first pattern group as a string
private _linkregex: string; // save the pattern as a string
private _linkre: RegExp; // save the compiled regex
constructor() {
const regex = vscode.workspace.getConfiguration().get('zettelView.regex');
this._h1regex = regex as string;
Expand All @@ -32,13 +34,16 @@ export class IDregex {
this._h1re = new RegExp(this._h1regex);
this._idregex = this._h1re.source.slice(3); // remove the ^# from the front
this._idre = new RegExp(this._idregex);

this._linkregex = '\\[\\[(' + this._idregex + ')\\]\\]'; // regex for markdown links
this._linkre = new RegExp(this._linkregex);
}

get h1re(): RegExp { return this._h1re; }
get h1regex(): string { return this._h1regex; }
get idre(): RegExp { return this._idre; }
get idregex(): string { return this._idregex; }
get linkre(): RegExp { return this._linkre; }
get linkregex(): string { return this._linkregex; }
}

// sadly, an object for compiled RegExp is needed by each ZettelViewTreeItem
Expand Down

0 comments on commit 2b5887d

Please sign in to comment.