Skip to content

Commit

Permalink
refactor: spilt hast-util-to-mdast from bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
hyoban committed Nov 14, 2024
1 parent 565b3ab commit ee558f5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
19 changes: 14 additions & 5 deletions apps/renderer/src/hooks/biz/useIntegrationActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,23 @@ export const useIntegrationActions = ({
const items = useMemo(() => {
if (!populatedEntry || view === undefined) return []

const getEntryContentAsMarkdown = () => {
const getEntryContentAsMarkdown = async () => {
const isReadabilityReady =
getReadabilityStatus()[populatedEntry.entries.id] === ReadabilityStatus.SUCCESS
const content =
(isReadabilityReady
? getReadabilityContent()[populatedEntry.entries.id].content
: populatedEntry.entries.content) || ""
return parseHtml(content).toMarkdown()

const [toMarkdown, toMdast, gfmTableToMarkdown] = await Promise.all([
import("mdast-util-to-markdown").then((m) => m.toMarkdown),
import("hast-util-to-mdast").then((m) => m.toMdast),
import("mdast-util-gfm-table").then((m) => m.gfmTableToMarkdown),
])

return toMarkdown(toMdast(parseHtml(content).hastTree), {
extensions: [gfmTableToMarkdown()],
})
}

const items: {
Expand Down Expand Up @@ -331,10 +340,10 @@ export const useIntegrationActions = ({
icon: <SimpleIconsObsidian />,
key: "saveToObsidian",
hide: !isObsidianEnabled || !populatedEntry?.entries?.url || !IN_ELECTRON,
onClick: () => {
onClick: async () => {
if (!isObsidianEnabled || !populatedEntry?.entries?.url || !IN_ELECTRON) return

const markdownContent = getEntryContentAsMarkdown()
const markdownContent = await getEntryContentAsMarkdown()
window.analytics?.capture("integration", {
type: "obsidian",
event: "save",
Expand Down Expand Up @@ -379,7 +388,7 @@ export const useIntegrationActions = ({
})
collectionId = collection.data.id
}
const markdownContent = getEntryContentAsMarkdown()
const markdownContent = await getEntryContentAsMarkdown()
await request("documents.create", {
title: populatedEntry.entries.title,
text: markdownContent,
Expand Down
4 changes: 0 additions & 4 deletions apps/renderer/src/lib/parse-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import type { Element, Parent, Text } from "hast"
import type { Schema } from "hast-util-sanitize"
import type { Components } from "hast-util-to-jsx-runtime"
import { toJsxRuntime } from "hast-util-to-jsx-runtime"
import { toMdast } from "hast-util-to-mdast"
import { toText } from "hast-util-to-text"
import { gfmTableToMarkdown } from "mdast-util-gfm-table"
import { toMarkdown } from "mdast-util-to-markdown"
import { createElement } from "react"
import { Fragment, jsx, jsxs } from "react/jsx-runtime"
import { renderToString } from "react-dom/server"
Expand Down Expand Up @@ -259,7 +256,6 @@ export const parseHtml = (
},
}),
toText: () => toText(hastTree),
toMarkdown: () => toMarkdown(toMdast(hastTree), { extensions: [gfmTableToMarkdown()] }),
}
}

Expand Down

0 comments on commit ee558f5

Please sign in to comment.