From bef80de315fd320d565196f17e658f4dbcbc69a1 Mon Sep 17 00:00:00 2001 From: Stephen Zhou <38493346+hyoban@users.noreply.github.com> Date: Wed, 23 Oct 2024 23:36:59 +0800 Subject: [PATCH] fix: convert html to markdown before save to obsidian --- apps/renderer/package.json | 2 + .../src/hooks/biz/useEntryActions.tsx | 3 +- apps/renderer/src/lib/parse-html.ts | 3 + pnpm-lock.yaml | 78 ++++++++++++++++++- 4 files changed, 84 insertions(+), 2 deletions(-) diff --git a/apps/renderer/package.json b/apps/renderer/package.json index 001f138ee..836aa56cd 100644 --- a/apps/renderer/package.json +++ b/apps/renderer/package.json @@ -62,6 +62,7 @@ "franc-min": "6.2.0", "fuse.js": "7.0.0", "hast-util-to-jsx-runtime": "2.3.2", + "hast-util-to-mdast": "^10.1.0", "hast-util-to-text": "4.0.2", "i18next": "^23.16.0", "i18next-browser-languagedetector": "8.0.0", @@ -72,6 +73,7 @@ "lethargy": "1.0.9", "lodash-es": "4.17.21", "masonic": "4.0.1", + "mdast-util-to-markdown": "^2.1.0", "nanoid": "5.0.7", "ofetch": "1.4.1", "path-to-regexp": "8.2.0", diff --git a/apps/renderer/src/hooks/biz/useEntryActions.tsx b/apps/renderer/src/hooks/biz/useEntryActions.tsx index 492fad32d..2eb04a7e7 100644 --- a/apps/renderer/src/hooks/biz/useEntryActions.tsx +++ b/apps/renderer/src/hooks/biz/useEntryActions.tsx @@ -33,6 +33,7 @@ import { shortcuts } from "~/constants/shortcuts" import { tipcClient } from "~/lib/client" import { nextFrame } from "~/lib/dom" import { FeedViewType } from "~/lib/enum" +import { parseHtml } from "~/lib/parse-html" import { getOS } from "~/lib/utils" import StarAnimationUri from "~/lottie/star.lottie?url" import type { CombinedEntryModel } from "~/models" @@ -427,7 +428,7 @@ export const useEntryActions = ({ saveToObsidian.mutate({ url: populatedEntry.entries.url, title: populatedEntry.entries.title || "", - content: populatedEntry.entries.content || "", + content: parseHtml(populatedEntry.entries.content || "").toMarkdown(), author: populatedEntry.entries.author || "", publishedAt: populatedEntry.entries.publishedAt || "", vaultPath: obsidianVaultPath, diff --git a/apps/renderer/src/lib/parse-html.ts b/apps/renderer/src/lib/parse-html.ts index 7474599e9..8e587fa0b 100644 --- a/apps/renderer/src/lib/parse-html.ts +++ b/apps/renderer/src/lib/parse-html.ts @@ -2,7 +2,9 @@ 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 { toMarkdown } from "mdast-util-to-markdown" import { createElement } from "react" import { Fragment, jsx, jsxs } from "react/jsx-runtime" import { renderToString } from "react-dom/server" @@ -235,6 +237,7 @@ export const parseHtml = ( }, }), toText: () => toText(hastTree), + toMarkdown: () => toMarkdown(toMdast(hastTree)), } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index eb7cb3cc6..f371989d2 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -477,6 +477,9 @@ importers: hast-util-to-jsx-runtime: specifier: 2.3.2 version: 2.3.2 + hast-util-to-mdast: + specifier: ^10.1.0 + version: 10.1.0 hast-util-to-text: specifier: 4.0.2 version: 4.0.2 @@ -507,6 +510,9 @@ importers: masonic: specifier: 4.0.1 version: 4.0.1(react@18.3.1) + mdast-util-to-markdown: + specifier: ^2.1.0 + version: 2.1.0 nanoid: specifier: 5.0.7 version: 5.0.7 @@ -3599,7 +3605,7 @@ packages: '@radix-ui/react-slot@1.1.0': resolution: {integrity: sha512-FUCf5XMfmW4dtYl69pdS4DbxKy8nj4M7SafBgPllysxmdachynNflAdp/gCsnYWNDnge6tI9onzMp5ARYc1KNw==} peerDependencies: - '@types/react': '*' + '@types/react': npm:types-react@19.0.0-rc.1 react: ^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc peerDependenciesMeta: '@types/react': @@ -6541,6 +6547,9 @@ packages: resolution: {integrity: sha512-0hJU9SCPvmMzIBdZFqNPXWa6dqh7WdH0cII9y+CyS8rG3nL48Bclra9HmKhVVUHyPWNH5Y7xDwAB7bfgSjkUMQ==} engines: {node: '>= 0.4'} + hast-util-embedded@3.0.0: + resolution: {integrity: sha512-naH8sld4Pe2ep03qqULEtvYr7EjrLK2QHY8KJR6RJkTUjPGObe1vnx585uzem2hGra+s1q08DZZpfgDVYRbaXA==} + hast-util-excerpt@2.0.0: resolution: {integrity: sha512-3hM4U3Tjvx8bn+KGM51BA2VL6poq3AyOHguq3dzDjI74lxYZfOV1zAEEiSbcGd8CXgx9N9JRS5MlS4izTWJRig==} @@ -6553,12 +6562,21 @@ packages: hast-util-has-property@3.0.0: resolution: {integrity: sha512-MNilsvEKLFpV604hwfhVStK0usFY/QmM5zX16bo7EjnAEGofr5YyI37kzopBlZJkHD4t887i+q/C8/tr5Q94cA==} + hast-util-is-body-ok-link@3.0.1: + resolution: {integrity: sha512-0qpnzOBLztXHbHQenVB8uNuxTnm/QBFUOmdOSsEn7GnBtyY07+ENTWVFBAnXd/zEgd9/SUG3lRY7hSIBWRgGpQ==} + hast-util-is-element@3.0.0: resolution: {integrity: sha512-Val9mnv2IWpLbNPqc/pUem+a7Ipj2aHacCwgNfTiK0vJKl0LF+4Ba4+v1oPHFpf3bLYmreq0/l3Gud9S5OH42g==} + hast-util-minify-whitespace@1.0.1: + resolution: {integrity: sha512-L96fPOVpnclQE0xzdWb/D12VT5FabA7SnZOUMtL1DbXmYiHJMXZvFkIZfiMmTCNJHUeO2K9UYNXoVyfz+QHuOw==} + hast-util-parse-selector@4.0.0: resolution: {integrity: sha512-wkQCkSYoOGCRKERFWcxMVMOcYE2K1AaNLU8DXS9arxnLOUEWbOXKXiJUNzEpqZ3JOKpnha3jkFrumEjVliDe7A==} + hast-util-phrasing@3.0.1: + resolution: {integrity: sha512-6h60VfI3uBQUxHqTyMymMZnEbNl1XmEGtOxxKYL7stY2o601COo62AWAYBQR9lZbYXYSBoxag8UpPRXK+9fqSQ==} + hast-util-sanitize@5.0.1: resolution: {integrity: sha512-IGrgWLuip4O2nq5CugXy4GI2V8kx4sFVy5Hd4vF7AR2gxS0N9s7nEAVUyeMtZKZvzrxVsHt73XdTsno1tClIkQ==} @@ -6571,6 +6589,9 @@ packages: hast-util-to-jsx-runtime@2.3.2: resolution: {integrity: sha512-1ngXYb+V9UT5h+PxNRa1O1FYguZK/XL+gkeqvp7EdHlB9oHUG0eYRo/vY5inBdcqo3RkPMC58/H94HvkbfGdyg==} + hast-util-to-mdast@10.1.0: + resolution: {integrity: sha512-DsL/SvCK9V7+vfc6SLQ+vKIyBDXTk2KLSbfBYkH4zeF/uR1yBajHRhkzuaUSGOB1WJSTieJBdHwxlC+HLKvZZw==} + hast-util-to-string@3.0.0: resolution: {integrity: sha512-OGkAxX1Ua3cbcW6EJ5pT/tslVb90uViVkcJ4ZZIMW/R33DX/AkcJcRrPebPwJkHYwlDHXz4aIwvAAaAdtrACFA==} @@ -8738,6 +8759,9 @@ packages: rehype-infer-description-meta@2.0.0: resolution: {integrity: sha512-8MZjKRfQiNB5+Qpt0fS9bpjcKe+nhF5GOTHo0M5e+XdStDpcZxkSMMnCfNIlpNaOyIQ7Hu1qEaUxm8ZL1lYI1g==} + rehype-minify-whitespace@6.0.2: + resolution: {integrity: sha512-Zk0pyQ06A3Lyxhe9vGtOtzz3Z0+qZ5+7icZ/PL/2x1SHPbKao5oB/g/rlc6BCTajqBb33JcOe71Ye1oFsuYbnw==} + rehype-parse@9.0.1: resolution: {integrity: sha512-ksCzCD0Fgfh7trPDxr2rSylbwq9iYDkSn8TCDmEJ49ljEUBxDVCzCHv7QNzZOfODanX4+bWQ4WZqLCRWYLfhag==} @@ -9395,6 +9419,9 @@ packages: resolution: {integrity: sha512-pkonvlKk8/ZuR0D5tLW8ljt5I8kmxp2XKymhepUeOdCEfKpZaktSArkLHZt76OB1ZvO9bssUsDty4SWhLvZpLg==} engines: {node: '>=0.10.0'} + trim-trailing-lines@2.1.0: + resolution: {integrity: sha512-5UR5Biq4VlVOtzqkm2AZlgvSlDJtME46uV0br0gENbwN4l5+mMKT4b9gJKqWtuL2zAIqajGJGuvbCbcAJUZqBg==} + trough@2.2.0: resolution: {integrity: sha512-tmMpK00BjZiUyVyvrBK7knerNgmgvcV/KLVyuma/SC+TQN167GrMRciANTz09+k3zW8L8t60jWO1GpfkZdjTaw==} @@ -16967,6 +16994,11 @@ snapshots: function-bind: 1.1.2 optional: true + hast-util-embedded@3.0.0: + dependencies: + '@types/hast': 3.0.4 + hast-util-is-element: 3.0.0 + hast-util-excerpt@2.0.0: dependencies: '@types/hast': 3.0.4 @@ -16996,14 +17028,34 @@ snapshots: dependencies: '@types/hast': 3.0.4 + hast-util-is-body-ok-link@3.0.1: + dependencies: + '@types/hast': 3.0.4 + hast-util-is-element@3.0.0: dependencies: '@types/hast': 3.0.4 + hast-util-minify-whitespace@1.0.1: + dependencies: + '@types/hast': 3.0.4 + hast-util-embedded: 3.0.0 + hast-util-is-element: 3.0.0 + hast-util-whitespace: 3.0.0 + unist-util-is: 6.0.0 + hast-util-parse-selector@4.0.0: dependencies: '@types/hast': 3.0.4 + hast-util-phrasing@3.0.1: + dependencies: + '@types/hast': 3.0.4 + hast-util-embedded: 3.0.0 + hast-util-has-property: 3.0.0 + hast-util-is-body-ok-link: 3.0.1 + hast-util-is-element: 3.0.0 + hast-util-sanitize@5.0.1: dependencies: '@types/hast': 3.0.4 @@ -17063,6 +17115,23 @@ snapshots: transitivePeerDependencies: - supports-color + hast-util-to-mdast@10.1.0: + dependencies: + '@types/hast': 3.0.4 + '@types/mdast': 4.0.4 + '@ungap/structured-clone': 1.2.0 + hast-util-phrasing: 3.0.1 + hast-util-to-html: 9.0.3 + hast-util-to-text: 4.0.2 + hast-util-whitespace: 3.0.0 + mdast-util-phrasing: 4.1.0 + mdast-util-to-hast: 13.2.0 + mdast-util-to-string: 4.0.0 + rehype-minify-whitespace: 6.0.2 + trim-trailing-lines: 2.1.0 + unist-util-position: 5.0.0 + unist-util-visit: 5.0.0 + hast-util-to-string@3.0.0: dependencies: '@types/hast': 3.0.4 @@ -19464,6 +19533,11 @@ snapshots: unist-util-remove-position: 5.0.0 vfile: 6.0.3 + rehype-minify-whitespace@6.0.2: + dependencies: + '@types/hast': 3.0.4 + hast-util-minify-whitespace: 1.0.1 + rehype-parse@9.0.1: dependencies: '@types/hast': 3.0.4 @@ -20197,6 +20271,8 @@ snapshots: dependencies: escape-string-regexp: 1.0.5 + trim-trailing-lines@2.1.0: {} + trough@2.2.0: {} truncate-utf8-bytes@1.0.2: