Skip to content

Commit

Permalink
✨ allow file:// for simple local slide
Browse files Browse the repository at this point in the history
  • Loading branch information
ebullient committed Dec 20, 2024
1 parent e9835fc commit 21b97df
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 10 deletions.
1 change: 1 addition & 0 deletions .markdownlint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ MD033:
allowed_elements:
- img
- a
MD034: false
4 changes: 2 additions & 2 deletions docs/content/credits.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ title: "Credits"
description: "contributors and packages used by Slides Extended"
---


## github contributors
{{%ghcontributors "ebullient/obsidian-slides-extended,MSzturc/obsidian-advanced-slides" %}}

{{%ghcontributors "ebullient/obsidian-slides-extended,MSzturc/obsidian-advanced-slides" %}}

## Packages and libraries

* [obsidian](https://obsidian.md/) - A knowledge base that works on local Markdown files
* [reveal-js](http://lab.hakim.se/reveal-js) - The HTML Presentation Framework
* [mermaid](https://knsv.github.io/mermaid) - generation of diagram and flowchart from text in a similar manner as markdown
Expand Down
11 changes: 4 additions & 7 deletions docs/content/features/embedSlide.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,17 @@ By using `slide` code blocks you are able to embed an slide into an obsidian not

#### Example

```md
````md
```slide
{
slide: [[Presentation]],
page: 7
slide: [[Presentation]],
page: 7
}
```
```
````

> [!TIP]
> When using horizontal & vertical slides you can specify a page as following: `3/6`
> This would show the 6th vertical slide on the 3rd horizontal position.



![Embed](../images/embed.png)
73 changes: 73 additions & 0 deletions src/obsidian/obsidianUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { readFileSync } from "fs-extra";
import {
type App,
FileSystemAdapter,
Platform,
type TFile,
resolveSubpath,
} from "obsidian";
Expand Down Expand Up @@ -257,6 +258,78 @@ export class ObsidianUtils implements MediaCollector {
return `![[${filename}#${header}]]`;
}

async fetchRemoteMarkdown(markdown: string): Promise<string> {
const stackTrace = Error().stack;
console.log(
"fetchRemoteMarkdown",
markdown.contains("file://"),
stackTrace,
);
const wikilinkFileRegex = /!\[\[(file:.+?\.md)(\|[^\]]+)?\]\]/gi;
const fileUrlRegex = /(!\[[^\]]*?\]\()(file:.+?\.md(?:#.+?)?)(\))/i;

// Replace wikilinks with markdown links
markdown = markdown.replace(wikilinkFileRegex, (match, p1, p2) => {
const filePath = p1;
const alias = p2 ? p2.slice(1) : "";
const url = new URL(filePath);
return `![${alias}](${url})`;
});
console.log(markdown);

// Replace markdown links with markdown content
if (fileUrlRegex.test(markdown)) {
const lines = markdown.split("\n");
for (let index = 0; index < lines.length; index++) {
const line = lines[index];
const match = fileUrlRegex.exec(line);
// one per line (as embed, should be fine)
if (match) {
const fullMatch = match[0];
const fileUrl = match[2];
const content = await this.readRemoteFile(fileUrl);
if (content) {
lines[index] = line.replace(fullMatch, content);
}
}
}
markdown = lines.join("\n");
}

return markdown;
}

async readRemoteFile(fileUrl: string): Promise<string> {
const anchorIndex = fileUrl.indexOf("#");
if (anchorIndex > -1) {
fileUrl = fileUrl.substring(0, anchorIndex);
}
const urlpath = fileUrl.replace("file://", Platform.resourcePathPrefix);

const result = await fetch(urlpath).catch((error) => {
return new Response(null, {
status: 404,
statusText: error.messge,
});
});
if (result.ok) {
if (result.blob) {
const blob = await result.blob();
const bytes = await blob.arrayBuffer();
const content = Buffer.from(bytes).toString();
if (this.yamlRegex.test(content)) {
return this.yamlRegex.exec(content)[1];
}
return content;
}
console.info(
"open a bug to handle this kind of response. Include this message",
result,
);
}
return null;
}

substring(
input: string,
startLine: number,
Expand Down
3 changes: 2 additions & 1 deletion src/reveal/revealRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ export class RevealRenderer {

const slidifyOptions = this.yaml.getSlidifyOptions(options);

const processedMarkdown = this.processor.process(markdown, options);
const prefetched = await this.utils.fetchRemoteMarkdown(markdown);
const processedMarkdown = this.processor.process(prefetched, options);
const slides = this.slidify(processedMarkdown, slidifyOptions);

const cssPaths = this.getCssPaths(options.css);
Expand Down
4 changes: 4 additions & 0 deletions src/reveal/revealServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export class RevealServer {
};

if (file.startsWith("local-file-url")) {
console.debug("fetching local file", file);
const urlpath = file.replace(
"local-file-url",
Platform.resourcePathPrefix,
Expand All @@ -99,18 +100,21 @@ export class RevealServer {
reply.code(404).send(result.statusText);
}
} else if (file.startsWith("embed/") && file.endsWith(".md")) {
console.debug("fetching embed file", file);
const filePath = path.join(
utils.vaultDirectory,
file.replace("embed/", ""),
);
await renderMarkdownFile(filePath);
} else if (file.endsWith(".md")) {
// top-level slide
console.debug("fetching markdown file", file);
this.filePath = path.join(utils.vaultDirectory, file);
await renderMarkdownFile(this.filePath);
} else {
let fetch = file;
const sourceDir = path.dirname(this.filePath);
console.debug("fetching other file", sourceDir);
if (sourceDir !== utils.vaultDirectory) {
const srcPath = path.join(sourceDir, file);
if (existsSync(srcPath)) {
Expand Down

0 comments on commit 21b97df

Please sign in to comment.