From f8aaa30f064decfb3e544dfcbc9c794a3599daab Mon Sep 17 00:00:00 2001 From: MSzturc Date: Wed, 8 Dec 2021 18:33:41 +0100 Subject: [PATCH] it is now possible to embed Markdown files --- src/main.ts | 2 +- src/multipleFileProcessor.ts | 94 +++++++++++++++++++++++++++++ src/obsidianMarkdownPreprocessor.ts | 21 +++++++ src/obsidianPreprocessor.ts | 10 +++ src/revealRenderer.ts | 37 +++++++----- src/revealServer.ts | 5 +- 6 files changed, 151 insertions(+), 18 deletions(-) create mode 100644 src/multipleFileProcessor.ts create mode 100644 src/obsidianMarkdownPreprocessor.ts create mode 100644 src/obsidianPreprocessor.ts diff --git a/src/main.ts b/src/main.ts index 9c0b58ff..06a6a6c2 100644 --- a/src/main.ts +++ b/src/main.ts @@ -15,7 +15,7 @@ export default class AdvancedSlidesPlugin extends Plugin { const fileSystemAdapter: FileSystemAdapter = this.app.vault.adapter as FileSystemAdapter; this.vaultDirectory = fileSystemAdapter.getBasePath(); - this.revealServer = new RevealServer(this.vaultDirectory); + this.revealServer = new RevealServer(this.app, this.vaultDirectory); this.revealServer.start(); this.registerView( diff --git a/src/multipleFileProcessor.ts b/src/multipleFileProcessor.ts new file mode 100644 index 00000000..aa2f8ad7 --- /dev/null +++ b/src/multipleFileProcessor.ts @@ -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; + } +} + + diff --git a/src/obsidianMarkdownPreprocessor.ts b/src/obsidianMarkdownPreprocessor.ts new file mode 100644 index 00000000..494f31a5 --- /dev/null +++ b/src/obsidianMarkdownPreprocessor.ts @@ -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; + } + + + +} + + diff --git a/src/obsidianPreprocessor.ts b/src/obsidianPreprocessor.ts new file mode 100644 index 00000000..2daeaf14 --- /dev/null +++ b/src/obsidianPreprocessor.ts @@ -0,0 +1,10 @@ + +export class ObsidianPreprocessor { + + constructor(vaultDir: String) { + } + + +} + + diff --git a/src/revealRenderer.ts b/src/revealRenderer.ts index a0147471..7e38484d 100644 --- a/src/revealRenderer.ts +++ b/src/revealRenderer.ts @@ -7,15 +7,20 @@ import { md } from "./markdown"; import _ from "lodash" import defaults from "./defaults.json"; +import { ObsidianMarkdownPreprocessor } from "./obsidianMarkdownPreprocessor"; +import { App } from "obsidian"; export class RevealRenderer { - private _vaultDirectory: string; - private _pluginDirectory: string; + private processor: ObsidianMarkdownPreprocessor; - constructor(baseDirectory: string) { - this._vaultDirectory = baseDirectory; - this._pluginDirectory = path.join(this._vaultDirectory, '/.obsidian/plugins/obsidian-advanced-slides/'); + private vaultDirectory: string; + private pluginDirectory: string; + + constructor(app: App, baseDirectory: string) { + this.vaultDirectory = baseDirectory; + this.pluginDirectory = path.join(this.vaultDirectory, '/.obsidian/plugins/obsidian-advanced-slides/'); + this.processor = new ObsidianMarkdownPreprocessor(app); } async renderFile(filePath: String) { @@ -31,7 +36,9 @@ export class RevealRenderer { const { title } = options; const themeUrl = this.getThemeUrl(options.theme); const highlightThemeUrl = this.getHighlightThemeUrl(options.highlightTheme); - const slides = this.slidify(markdown, this.getSlidifyOptions(options)); + + const processedMarkdown = this.processor.process(markdown); + const slides = this.slidify(processedMarkdown, this.getSlidifyOptions(options)); const context = Object.assign(options, { title, @@ -54,13 +61,13 @@ export class RevealRenderer { return theme; } catch (err) { } - const highlightThemes = glob.sync('plugin/highlight/*.css', { cwd: this._pluginDirectory }); - + const highlightThemes = glob.sync('plugin/highlight/*.css', { cwd: this.pluginDirectory }); + const highlightTheme = highlightThemes.find( themePath => path.basename(themePath).replace(path.extname(themePath), '') === theme - ); + ); - return highlightTheme ? '/' + highlightTheme : '/' + theme; + return highlightTheme ? '/' + highlightTheme : '/' + theme; } private getThemeUrl(theme: string) { @@ -69,17 +76,17 @@ export class RevealRenderer { return theme; } catch (err) { } - const revealThemes = glob.sync('dist/theme/*.css', { cwd: this._pluginDirectory }); - + const revealThemes = glob.sync('dist/theme/*.css', { cwd: this.pluginDirectory }); + const revealTheme = revealThemes.find( themePath => path.basename(themePath).replace(path.extname(themePath), '') === theme - ); + ); - return revealTheme ? '/' + revealTheme : '/' + theme; + return revealTheme ? '/' + revealTheme : '/' + theme; } private async getTemplate() { - const templateFile = path.join(this._pluginDirectory, defaults.template); + const templateFile = path.join(this.pluginDirectory, defaults.template); const content = (await fs.readFile(templateFile.toString())).toString(); return content; } diff --git a/src/revealServer.ts b/src/revealServer.ts index 102125b3..acb17361 100644 --- a/src/revealServer.ts +++ b/src/revealServer.ts @@ -2,6 +2,7 @@ import express from "express"; import path from 'path'; import { Server } from "http"; import { RevealRenderer } from "./revealRenderer"; +import { App } from "obsidian"; export class RevealServer { @@ -13,11 +14,11 @@ export class RevealServer { private _revealRenderer: RevealRenderer; private _staticDir = express.static; - constructor(vaultDir: String) { + constructor(app: App, vaultDir: String) { this._baseDirectory = vaultDir.toString(); this._pluginDirectory = path.join(this._baseDirectory, '/.obsidian/plugins/obsidian-advanced-slides/'); this._app = express(); - this._revealRenderer = new RevealRenderer(this._baseDirectory); + this._revealRenderer = new RevealRenderer(app, this._baseDirectory); } getUrl() {