Skip to content

Commit da4e6b9

Browse files
committed
increment_smart_env_version_to_2_139102_and_add_obsidian_markdown_source_content_adapter
- Bump SmartEnv version to 2.139102 to reflect recent changes. - Introduce ObsidianMarkdownSourceContentAdapter for enhanced handling of markdown files, including optional HTML rendering and conversion back to markdown. - Implement metadata retrieval and dynamic rendering capabilities, improving integration with Obsidian's environment. - Maintain existing comments and methods unrelated to the changes.
1 parent 5790a9b commit da4e6b9

2 files changed

Lines changed: 65 additions & 3 deletions

File tree

smart-environment/smart_env.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class SmartEnv {
4141
* If a newer version is loaded into a runtime that already has an older environment,
4242
* an automatic reload of all existing mains will occur.
4343
*/
44-
static version = 2.139101;
44+
static version = 2.139102;
4545
scope_name = 'smart_env';
4646
static global_ref = get_global_ref();
4747
global_ref = this.constructor.global_ref;
Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,76 @@
11
import { MarkdownSourceContentAdapter } from "./markdown_source.js";
2+
import { MarkdownRenderer, htmlToMarkdown, Component } from 'obsidian';
23

4+
/**
5+
* @class ObsidianMarkdownSourceContentAdapter
6+
* @extends MarkdownSourceContentAdapter
7+
* @description
8+
* An adapter for Obsidian markdown files that can optionally render
9+
* the file content to HTML and convert it back to markdown to capture
10+
* dynamic transformations (e.g. Dataview).
11+
*/
312
export class ObsidianMarkdownSourceContentAdapter extends MarkdownSourceContentAdapter {
13+
/**
14+
* Returns the frontmatter metadata from Obsidian's metadataCache.
15+
* @async
16+
* @returns {Promise<Object>} Frontmatter data if available, otherwise undefined.
17+
*/
418
async get_metadata() {
519
const app = this.item.env.main.app;
6-
const {frontmatter} = app.metadataCache.getFileCache(this.item.file);
20+
const { frontmatter } = app.metadataCache.getFileCache(this.item.file) || {};
721
return frontmatter;
822
}
23+
24+
/**
25+
* Reads the file content. If opts.render_output is true, attempts to use
26+
* Obsidian's MarkdownRenderer to render the file to HTML, then convert it
27+
* back to markdown via htmlToMarkdown.
28+
* @async
29+
* @param {Object} [opts={}] - Options for reading.
30+
* @param {boolean} [opts.render_output=false] - If true, render MD -> HTML -> MD.
31+
* @returns {Promise<string>} The file content (possibly rendered).
32+
*/
33+
async read(opts = {}) {
34+
const content = await super.read(opts);
35+
if (!opts.render_output) {
36+
return content;
37+
}
38+
39+
// Attempt dynamic rendering
40+
const app = this.item.env.main.app;
41+
if (!app || !MarkdownRenderer || !htmlToMarkdown) {
42+
console.warn('Obsidian environment not found; cannot render markdown.');
43+
return content;
44+
}
45+
46+
// Render to HTML
47+
const container = document.createElement('div');
48+
// Obsidian's signature: renderMarkdown(markdown, container, sourcePath, plugin)
49+
await MarkdownRenderer.render(app, content, container, this.item.path, new Component());
50+
51+
// wait for container to stop changing
52+
let last_html = container.innerHTML;
53+
const max_wait = 10000;
54+
let wait_time = 0;
55+
while (last_html !== container.innerHTML || last_html.includes('Loading...')) {
56+
console.log('waiting for markdown to render');
57+
await new Promise(resolve => setTimeout(resolve, 200));
58+
last_html = container.innerHTML;
59+
wait_time += 200;
60+
if (wait_time > max_wait) {
61+
console.warn('ObsidianMarkdownSourceContentAdapter: Timeout waiting for markdown to render.');
62+
break;
63+
}
64+
}
65+
66+
// Convert HTML back to MD
67+
const newMd = htmlToMarkdown(container);
68+
console.log('newMd', newMd);
69+
return newMd;
70+
}
971
}
1072

1173
export default {
1274
collection: null, // No collection adapter needed for markdown sources
1375
item: ObsidianMarkdownSourceContentAdapter
14-
};
76+
};

0 commit comments

Comments
 (0)