-
-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
it is now possible to embed Markdown files
- Loading branch information
Showing
6 changed files
with
151 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import fs from "fs-extra"; | ||
import { App, FileSystemAdapter } from "obsidian"; | ||
|
||
export class MultipleFileProcessor { | ||
|
||
private app: App; | ||
|
||
private regex = /!\[\[(.*)\]\]/gm; | ||
|
||
constructor(app: App) { | ||
this.app = app; | ||
} | ||
|
||
process(markdown: string){ | ||
return markdown | ||
.split('\n') | ||
.map((line, index) => { | ||
if (this.regex.test(line)) | ||
return this.transformLine(line); | ||
return line; | ||
}) | ||
.join('\n'); | ||
} | ||
|
||
transformLine (line) { | ||
var filePath = line.replace("![[", "").replace("]]", ""); | ||
var header = null; | ||
|
||
if (filePath.includes('#')) { | ||
const split = filePath.split('#'); | ||
filePath = split[0]; | ||
header = split[1]; | ||
} | ||
|
||
const res = this.getMarkdownFile(filePath); | ||
|
||
if (res === null) { | ||
return line; | ||
} | ||
|
||
return this.process(this.parseFile(res, header)); | ||
} | ||
|
||
parseFile(file, header) { | ||
|
||
var fileContent = fs.readFileSync(file, {encoding: 'utf-8'}); | ||
|
||
if (header === null) { | ||
return fileContent; | ||
} else { | ||
|
||
var lines = fileContent.split('\n'); | ||
|
||
var startIdx = null; | ||
var endIdx = null; | ||
for (let i = 0; i < lines.length; i++) { | ||
|
||
if (startIdx != null && lines[i].startsWith('#')) { | ||
endIdx = i; | ||
break; | ||
} | ||
|
||
if (lines[i].includes(header)) { | ||
startIdx = i; | ||
} | ||
} | ||
|
||
if (startIdx === null) { | ||
return "![[" + file + "#" + header + "]]"; | ||
} | ||
|
||
if (endIdx === null) { | ||
return lines.slice(startIdx).join('\n'); | ||
} else { | ||
return lines.slice(startIdx, endIdx).join('\n'); | ||
} | ||
} | ||
} | ||
|
||
getMarkdownFile(line) { | ||
var file = line; | ||
if (!line.toLowerCase().endsWith(".md")) { | ||
file = file + ".md"; | ||
} | ||
|
||
const adapter = this.app.vault.adapter as FileSystemAdapter; | ||
const markdownFile = this.app.vault.getMarkdownFiles().filter(item => item.path.contains(file)).first(); | ||
const filePath = adapter.getFullPath(markdownFile.path); | ||
|
||
return filePath; | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { App } from "obsidian"; | ||
import { MultipleFileProcessor } from "./multipleFileProcessor"; | ||
|
||
export class ObsidianMarkdownPreprocessor { | ||
|
||
private multipleFileProcessor : MultipleFileProcessor; | ||
|
||
constructor(app: App) { | ||
this.multipleFileProcessor = new MultipleFileProcessor(app); | ||
} | ||
|
||
process(markdown: string){ | ||
const afterMultipleFileProcessor = this.multipleFileProcessor.process(markdown); | ||
return afterMultipleFileProcessor; | ||
} | ||
|
||
|
||
|
||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
|
||
export class ObsidianPreprocessor { | ||
|
||
constructor(vaultDir: String) { | ||
} | ||
|
||
|
||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters