Skip to content

Commit 69cbf3c

Browse files
ChivierJichouP
authored andcommitted
feat: automatic replace wikilink to desired link
1 parent 0ec1fe6 commit 69cbf3c

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/preview.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,27 @@ export class PreviewView extends ItemView implements PreviewViewState {
3434
return 'Marp Preview';
3535
}
3636

37+
38+
// Function to replace Wikilinks with the desired format
39+
replaceImageWikilinks(markdown: string): string {
40+
const wikilinkRegex = /!\[\[([^\]]+)\]\]/g;
41+
const lines = markdown.split("\n");
42+
const replacedLines = lines.map((line) => {
43+
const replacedLine = line.replace(wikilinkRegex, (match, name) => {
44+
// Modify the format of the Wikilink as needed
45+
const url = this.app.vault.adapter.getResourcePath(name);
46+
return `![${name}](${url})`;
47+
});
48+
return replacedLine;
49+
});
50+
return replacedLines.join("\n");
51+
}
52+
53+
3754
async renderPreview() {
3855
if (!this.file) return;
39-
const content = await this.app.vault.cachedRead(this.file);
56+
const originContent = await this.app.vault.cachedRead(this.file);
57+
const content = this.replaceImageWikilinks(originContent);
4058
const { html, css } = marp.render(content);
4159
const doc = await convertHtml(html);
4260
const container = this.containerEl.children[1];

0 commit comments

Comments
 (0)