Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Use renderMarkdown native Obsidian fonction #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@
"react-dom": "^18.2.0",
"rollup": "^2.75.5",
"rollup-plugin-terser": "^7.0.2",
"semantic-release": "^19.0.5",
"showdown": "^2.1.0"
"semantic-release": "^19.0.5"
},
"release": {
"tagFormat": "${version}",
Expand Down
11 changes: 1 addition & 10 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
import showdown from 'showdown';

const removeFrontmatter = (markdown) => markdown?.replace(/^---[\s\S]+?---/, '');

export const markdownToHtml = (markdown) => {
const converter = new showdown.Converter();
const md = removeFrontmatter(markdown);
converter.setOption('simpleLineBreaks', true);
return converter.makeHtml(md);
};
export const removeFrontmatter = (markdown) => markdown?.replace(/^---[\s\S]+?---/, '');

export const countWords = (content) => (content.match(/[^\s]+/g) || []).length;
4 changes: 2 additions & 2 deletions src/views/chapters.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ export default class ChaptersView extends ItemView {
});

navFile.addEventListener('click', async () => {
const targetFile = this.app.vault.getMarkdownFiles().find((f) => f.path === `${chapter}.md`)
const targetFile = this.app.vault.getAbstractFileByPath(`${chapter}.md`)
|| await this.app.vault.create(`${chapter}.md`, '');
const leaf = this.app.workspace.getMostRecentLeaf();
const leaf = this.app.workspace.getLeaf();
leaf.openFile(targetFile);
});
}
Expand Down
7 changes: 4 additions & 3 deletions src/views/goal.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ItemView, parseFrontMatterEntry } from 'obsidian'; // eslint-disable-line
import { ItemView, parseFrontMatterEntry, MarkdownRenderer } from 'obsidian'; // eslint-disable-line
import * as React from 'react';
import { createRoot } from 'react-dom/client';

import Goal from '../components/goal';
import Previewer from '../components/previewer';
import { countWords, markdownToHtml } from '../utils';
import { countWords, removeFrontmatter } from '../utils';
import { ICON_NAME, DEFAULT_GOAL, VIEW_TYPE_GOAL } from '../constants';

export default class GoalView extends ItemView {
Expand Down Expand Up @@ -55,7 +55,8 @@ export default class GoalView extends ItemView {

const activeFile = await this.app.workspace.getActiveFile();
const markdown = activeFile ? await this.app.vault.cachedRead(activeFile) : false;
const content = markdownToHtml(markdown);
const markdownWithoutFrontmatter = removeFrontmatter(markdown);
const content = await MarkdownRenderer.renderMarkdown(markdownWithoutFrontmatter);

return this.root.render(
<>
Expand Down