Skip to content

Commit

Permalink
refactor: use markdownrenderer native function
Browse files Browse the repository at this point in the history
  • Loading branch information
johackim committed Dec 7, 2022
1 parent fb450e4 commit d1aedde
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 15 deletions.
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;
8 changes: 5 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,9 @@ 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

0 comments on commit d1aedde

Please sign in to comment.