From 491bf776d47f78a160322174eea5f49f545fc504 Mon Sep 17 00:00:00 2001 From: johackim Date: Wed, 7 Dec 2022 16:57:00 +0100 Subject: [PATCH] refactor: use markdownrenderer native function --- package.json | 3 +-- src/utils.js | 11 +---------- src/views/goal.js | 7 ++++--- 3 files changed, 6 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index f9a2431..624a025 100644 --- a/package.json +++ b/package.json @@ -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}", diff --git a/src/utils.js b/src/utils.js index 6782e51..662de62 100644 --- a/src/utils.js +++ b/src/utils.js @@ -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; diff --git a/src/views/goal.js b/src/views/goal.js index bc69a9b..225e6d7 100644 --- a/src/views/goal.js +++ b/src/views/goal.js @@ -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 { @@ -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( <>