Skip to content

Commit

Permalink
added support for internal links
Browse files Browse the repository at this point in the history
  • Loading branch information
MSzturc committed Dec 9, 2021
1 parent ba5aa0c commit 2f4072e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
28 changes: 28 additions & 0 deletions src/internalLinkProcessor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

export class InternalLinkProcessor {

process(markdown: string) {
return this.transformInternalLink(markdown);
}

private transformInternalLink(line: string): string {

if (line.includes('[[')) {
var startIdx = line.indexOf('[[');
var endIdx = line.indexOf(']]', startIdx + 3);
var linkContent = line.substring(startIdx + 2, endIdx).trim();

if (linkContent.includes('|')) {
linkContent = linkContent.split('|')[1];
}
var result = line.substring(0, startIdx) + linkContent + line.substring(endIdx + 2);

return this.transformInternalLink(result);
}

return line;
}

}


6 changes: 5 additions & 1 deletion src/obsidianMarkdownPreprocessor.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
import { App } from "obsidian";
import { BlockProcessor } from "./blockProcessor";
import { ImageProcessor } from "./imageProcessor";
import { InternalLinkProcessor } from "./internalLinkProcessor";
import { MultipleFileProcessor } from "./multipleFileProcessor";

export class ObsidianMarkdownPreprocessor {

private multipleFileProcessor : MultipleFileProcessor;
private blockProcessor : BlockProcessor;
private imageProcessor : ImageProcessor;
private internalLinkProcessor : InternalLinkProcessor;

constructor(app: App) {
this.multipleFileProcessor = new MultipleFileProcessor(app);
this.blockProcessor = new BlockProcessor();
this.imageProcessor = new ImageProcessor(app);
this.internalLinkProcessor = new InternalLinkProcessor();
}

process(markdown: string){
const afterMultipleFileProcessor = this.multipleFileProcessor.process(markdown);
const afterBlockProcessor = this.blockProcessor.process(afterMultipleFileProcessor);

const afterImageProcessor = this.imageProcessor.process(afterBlockProcessor);
return afterImageProcessor;
const afterInternalLinkProcessor = this.internalLinkProcessor.process(afterImageProcessor);
return afterInternalLinkProcessor;
}


Expand Down

0 comments on commit 2f4072e

Please sign in to comment.