Skip to content

Commit

Permalink
it is now possible to embed Markdown files
Browse files Browse the repository at this point in the history
  • Loading branch information
MSzturc committed Dec 8, 2021
1 parent bbf5b52 commit f8aaa30
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
94 changes: 94 additions & 0 deletions src/multipleFileProcessor.ts
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;
}
}


21 changes: 21 additions & 0 deletions src/obsidianMarkdownPreprocessor.ts
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;
}



}


10 changes: 10 additions & 0 deletions src/obsidianPreprocessor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

export class ObsidianPreprocessor {

constructor(vaultDir: String) {
}


}


37 changes: 22 additions & 15 deletions src/revealRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
Expand All @@ -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) {
Expand All @@ -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;
}
Expand Down
5 changes: 3 additions & 2 deletions src/revealServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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() {
Expand Down

0 comments on commit f8aaa30

Please sign in to comment.