Skip to content

⚡️⚡️⚡️ Self-hosted collaborative bookmark manager to collect, read, annotate, and fully preserve what matters, all in one place.

support for markdown for readable content

daniel31x136573c683f66e
diff --git a/components/ReadableView.tsx b/components/ReadableView.tsx
index 6b63149c..c528da2b 100644
--- a/components/ReadableView.tsx
+++ b/components/ReadableView.tsx
@@ -13,18 +13,33 @@ import isValidUrl from "@/lib/shared/isValidUrl";
import Link from "next/link";
import unescapeString from "@/lib/client/unescapeString";
import usePermissions from "@/hooks/usePermissions";
+import showdown from "showdown";
+import TurndownService from "turndown";
-type LinkContent = {
- title: string;
- content: string;
- textContent: string;
- length: number;
- excerpt: string;
- byline: string;
- dir: string;
- siteName: string;
- lang: string;
-};
+const turndownService = new TurndownService({
+ headingStyle: "atx",
+ bulletListMarker: "-",
+ emDelimiter: "*",
+});
+
+const converter = new showdown.Converter({
+ omitExtraWLInCodeBlocks: true,
+ noHeaderId: false,
+ parseImgDimensions: true,
+ simplifiedAutoLink: true,
+ literalMidWordUnderscores: true,
+ strikethrough: true,
+ tables: true,
+ tablesHeaderId: false,
+ ghCodeBlocks: true,
+ tasklists: true,
+ smoothLivePreview: true,
+ prefixHeaderId: false,
+ disableForced4SpacesIndentedSublists: false,
+ ghCompatibleHeaderId: true,
+ smartIndentationFix: false,
+ headerLevelStart: 1,
+});
type Props = {
link: LinkIncludingShortenedCollectionAndTags;
@@ -32,9 +47,14 @@ type Props = {
export default function ReadableView({ link }: Props) {
const { t } = useTranslation();
- const [linkContent, setLinkContent] = useState<LinkContent>();
+ const [linkContent, setLinkContent] = useState("");
+
+ const [isEditing, setIsEditing] = useState(false);
+ const [editedMarkdown, setEditedMarkdown] = useState("");
const router = useRouter();
+ const isPublicRoute = router.pathname.startsWith("/public");
+ const permissions = usePermissions(link?.collection?.id as number);
useEffect(() => {
const fetchLinkContent = async () => {
@@ -42,19 +62,49 @@ export default function ReadableView({ link }: Props) {
const response = await fetch(
`/api/v1/archives/${link?.id}?format=${ArchivedFormat.readability}`
);
-
const data = await response?.json();
-
- setLinkContent(data);
+ setLinkContent(data?.content);
}
};
fetchLinkContent();
- }, [link]);
+ }, [link, router.query.id]);
- const isPublicRoute = router.pathname.startsWith("/public");
+ useEffect(() => {
+ if (!isEditing && linkContent) {
+ const initialMarkdown = turndownService.turndown(linkContent);
+ setEditedMarkdown(initialMarkdown);
+ }
+ }, [linkContent, isEditing]);
- const permissions = usePermissions(link?.collection?.id as number);
+ const startEditing = () => {
+ if (linkContent) {
+ const initialMarkdown = turndownService.turndown(linkContent);
+ setEditedMarkdown(initialMarkdown);
+ }
+ setIsEditing(true);
+ };
+
+ const cancelEditing = () => {
+ setIsEditing(false);
+ };
+
+ const saveChanges = () => {
+ const updatedHTML = DOMPurify.sanitize(converter.makeHtml(editedMarkdown));
+
+ console.log("Saved HTML content:", updatedHTML);
+
+ setLinkContent(updatedHTML);
+
+ setIsEditing(false);
+
+ // If you want to persist it somewhere:
+ // await fetch('/api/saveHtml', {
+ // method: 'PUT',
+ // body: JSON.stringify({ html: updatedHTML }),
+ // headers: { 'Content-Type': 'application/json' },
+ // });
+ };
return (
<div className="flex flex-col gap-3 items-start p-3">
@@ -77,23 +127,63 @@ export default function ReadableView({ link }: Props) {
</div>
</div>
- <div className="text-sm text-neutral flex justify-between md:flex-row flex-col md:items-center w-full gap-2">
+ <div className="text-sm text-neutral flex justify-between w-full gap-2">
<LinkDate link={link} />
+
+ {!isPublicRoute && (permissions === true || permissions?.canUpdate) && (
+ <>
+ {!isEditing ? (
+ <button
+ className="flex items-center gap-2 btn btn-sm"
+ onClick={startEditing}
+ >
+ <i className="bi-pencil" />
+ {t("edit")}
+ </button>
+ ) : (
+ <div className="flex items-center gap-2">
+ <button
+ className="flex items-center gap-2 btn btn-sm"
+ onClick={cancelEditing}
+ >
+ <i className="bi-x text-xl" />
+ </button>
+ <button
+ className="flex items-center gap-2 btn btn-primary btn-sm"
+ onClick={saveChanges}
+ >
+ <i className="bi-check2 text-xl" />
+ </button>
+ </div>
+ )}
+ </>
+ )}
</div>
+
{link?.readable?.startsWith("archives") ? (
- <div
- className={clsx(
- "px-3 rounded-md w-full",
- linkContent?.content && "bg-base-200"
- )}
- >
- {linkContent?.content ? (
- <div
- className="line-break px-1 reader-view"
- dangerouslySetInnerHTML={{
- __html: DOMPurify.sanitize(linkContent?.content) || "",
- }}
- ></div>
+ <>
+ {linkContent ? (
+ isEditing ? (
+ <textarea
+ className="h-[50vh] w-full rounded-md p-3 border-neutral-content bg-base-200 focus:border-primary border-solid border outline-none duration-100"
+ value={editedMarkdown}
+ onChange={(e) => setEditedMarkdown(e.target.value)}
+ />
+ ) : (
+ <div
+ className={clsx(
+ "p-3 rounded-md w-full",
+ linkContent && "bg-base-200"
+ )}
+ >
+ <div
+ className="line-break px-1 reader-view"
+ dangerouslySetInnerHTML={{
+ __html: DOMPurify.sanitize(linkContent),
+ }}
+ />
+ </div>
+ )
) : (
<div className="p-5 m-auto w-full flex flex-col items-center gap-5">
<div className="w-full mr-auto h-4 skeleton rounded-md"></div>
@@ -107,7 +197,7 @@ export default function ReadableView({ link }: Props) {
<div className="w-5/6 mr-auto h-4 skeleton rounded-md"></div>
</div>
)}
- </div>
+ </>
) : (
<div className={`w-full h-full flex flex-col justify-center`}>
<svg
diff --git a/package.json b/package.json
index b9eac647..49b672ac 100644
--- a/package.json
+++ b/package.json
@@ -82,9 +82,11 @@
"react-window": "^1.8.10",
"rss": "^1.2.2",
"rss-parser": "^3.13.0",
+ "showdown": "^2.1.0",
"socks-proxy-agent": "^8.0.2",
"stripe": "^12.13.0",
"tailwind-merge": "^2.3.0",
+ "turndown": "^7.2.0",
"vaul": "^1.1.1",
"zod": "^3.23.8",
"zustand": "^4.3.8"
@@ -97,6 +99,8 @@
"@types/node-fetch": "^2.6.10",
"@types/react-window": "^1.8.8",
"@types/shelljs": "^0.8.15",
+ "@types/showdown": "^2.0.6",
+ "@types/turndown": "^5.0.5",
"autoprefixer": "^10.4.14",
"daisyui": "^4.4.2",
"nodemon": "^3.0.2",
diff --git a/pages/api/v1/archives/[linkId].ts b/pages/api/v1/archives/[linkId].ts
index 91d0bb06..03fa1a16 100644
--- a/pages/api/v1/archives/[linkId].ts
+++ b/pages/api/v1/archives/[linkId].ts
@@ -12,6 +12,7 @@ import verifyToken from "@/lib/api/verifyToken";
import generatePreview from "@/lib/api/generatePreview";
import createFolder from "@/lib/api/storage/createFolder";
import { UploadFileSchema } from "@/lib/shared/schemaValidation";
+import DOMPurify from "dompurify";
export const config = {
api: {
diff --git a/yarn.lock b/yarn.lock
index 70595678..44c93423 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -1186,6 +1186,11 @@
semver "^7.3.5"
tar "^6.1.11"
+"@mixmark-io/domino@^2.2.0":
+ version "2.2.0"
+ resolved "https://registry.yarnpkg.com/@mixmark-io/domino/-/domino-2.2.0.tgz#4e8ec69bf1afeb7a14f0628b7e2c0f35bdb336c3"
+ integrity sha512-Y28PR25bHXUg88kCV7nivXrP2Nj2RueZ3/l/jdx6J9f8J4nsEGcgX0Qe6lt7Pa+J79+kPiJU3LguR6O/6zrLOw==
+
"@mozilla/readability@^0.4.4":
version "0.4.4"
resolved "https://registry.yarnpkg.com/@mozilla/readability/-/readability-0.4.4.tgz#5d258b3436eba1d5fe31c4386e50a848d5cb5811"
@@ -2124,6 +2129,11 @@
"@types/glob" "~7.2.0"
"@types/node" "*"
+"@types/showdown@^2.0.6":
+ version "2.0.6"
+ resolved "https://registry.yarnpkg.com/@types/showdown/-/showdown-2.0.6.tgz#3d7affd5f971b4a17783ec2b23b4ad3b97477b7e"
+ integrity sha512-pTvD/0CIeqe4x23+YJWlX2gArHa8G0J0Oh6GKaVXV7TAeickpkkZiNOgFcFcmLQ5lB/K0qBJL1FtRYltBfbGCQ==
+
"@types/tough-cookie@*":
version "4.0.3"
resolved "https://registry.yarnpkg.com/@types/tough-cookie/-/tough-cookie-4.0.3.tgz#3d06b6769518450871fbc40770b7586334bdfd90"
@@ -2134,6 +2144,11 @@
resolved "https://registry.yarnpkg.com/@types/trusted-types/-/trusted-types-2.0.5.tgz#5cac7e7df3275bb95f79594f192d97da3b4fd5fe"
integrity sha512-I3pkr8j/6tmQtKV/ZzHtuaqYSQvyjGRKH4go60Rr0IDLlFxuRT5V32uvB1mecM5G1EVAUyF/4r4QZ1GHgz+mxA==
+"@types/turndown@^5.0.5":
+ version "5.0.5"
+ resolved "https://registry.yarnpkg.com/@types/turndown/-/turndown-5.0.5.tgz#614de24fc9ace4d8c0d9483ba81dc8c1976dd26f"
+ integrity sha512-TL2IgGgc7B5j78rIccBtlYAnkuv8nUQqhQc+DSYV5j9Be9XOcm/SKOVRuA47xAVI3680Tk9B1d8flK2GWT2+4w==
+
"@typescript-eslint/parser@^5.42.0":
version "5.49.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-5.49.0.tgz#d699734b2f20e16351e117417d34a2bc9d7c4b90"
@@ -2692,6 +2707,11 @@ commander@^4.0.0:
resolved "https://registry.yarnpkg.com/commander/-/commander-4.1.1.tgz#9fd602bd936294e9e9ef46a3f4d6964044b18068"
integrity sha512-NOKm8xhkzAjzFx8B2v5OAHT+u5pRQc2UCa2Vq9jYL/31o2wi9mxBA7LIFs3sV5VSC49z6pEhfbMULvShKj26WA==
+commander@^9.0.0:
+ version "9.5.0"
+ resolved "https://registry.yarnpkg.com/commander/-/commander-9.5.0.tgz#bc08d1eb5cedf7ccb797a96199d41c7bc3e60d30"
+ integrity sha512-KRs7WVDKg86PWiuAqhDrAQnTXZKraVcCc6vFdL14qrZ/DcWwuRo7VoiYXalXO7S5GKpqYiVEwCbgFDfxNHKJBQ==
+
version "0.0.1"
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"
@@ -5741,6 +5761,13 @@ shell-quote@^1.8.1:
resolved "https://registry.yarnpkg.com/shell-quote/-/shell-quote-1.8.1.tgz#6dbf4db75515ad5bac63b4f1894c3a154c766680"
integrity sha512-6j1W9l1iAs/4xYBI1SYOVZyFcCis9b4KCLQ8fgAGG07QvzaRLVVRQvAy85yNmmZSjYjg4MWh4gNvlPujU/5LpA==
+showdown@^2.1.0:
+ version "2.1.0"
+ resolved "https://registry.yarnpkg.com/showdown/-/showdown-2.1.0.tgz#1251f5ed8f773f0c0c7bfc8e6fd23581f9e545c5"
+ integrity sha512-/6NVYu4U819R2pUIk79n67SYgJHWCce0a5xTP979WbNp0FL9MN1I1QK662IDU1b6JzKTvmhgI7T7JYIxBi3kMQ==
+ dependencies:
+ commander "^9.0.0"
+
side-channel@^1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/side-channel/-/side-channel-1.0.4.tgz#efce5c8fdc104ee751b25c58d4290011fa5ea2cf"
@@ -6242,6 +6269,13 @@ tunnel-agent@^0.6.0:
dependencies:
safe-buffer "^5.0.1"
+turndown@^7.2.0:
+ version "7.2.0"
+ resolved "https://registry.yarnpkg.com/turndown/-/turndown-7.2.0.tgz#67d614fe8371fb511079a93345abfd156c0ffcf4"
+ integrity sha512-eCZGBN4nNNqM9Owkv9HAtWRYfLA4h909E/WGAWWBpmB275ehNhZyk87/Tpvjbp0jjNl9XwCsbe6bm6CqFsgD+A==
+ dependencies:
+ "@mixmark-io/domino" "^2.2.0"
+
tweetnacl@^0.14.3, tweetnacl@~0.14.0:
version "0.14.5"
resolved "https://registry.yarnpkg.com/tweetnacl/-/tweetnacl-0.14.5.tgz#5ae68177f192d4456269d108afa93ff8743f4f64"