Skip to content

Commit

Permalink
Fix spoiler summary inline formatting (aeharding#1546)
Browse files Browse the repository at this point in the history
For example, bold/italic text in spoiler summary now works (used by lemmy.world media bias bot).

Also refactored internal spoiler syntax tree.
  • Loading branch information
aeharding authored Jul 31, 2024
1 parent 0f0468a commit be513d1
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 93 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
}
},
"dependencies": {
"@aeharding/remark-lemmy-spoiler": "^1.0.1",
"@aeharding/remark-lemmy-spoiler": "^2.0.0",
"@capacitor-community/app-icon": "^5.0.0",
"@capacitor/android": "^6.1.0",
"@capacitor/app": "^6.0.0",
Expand Down Expand Up @@ -73,7 +73,6 @@
"mdast-util-gfm-autolink-literal-lemmy": "^3.0.0",
"mdast-util-gfm-strikethrough": "^2.0.0",
"mdast-util-gfm-table": "^2.0.0",
"mdast-util-lemmy-spoiler": "^1.0.0",
"micromark": "^4.0.0",
"micromark-extension-gfm-strikethrough": "^2.1.0",
"micromark-extension-gfm-table": "^2.1.0",
Expand Down Expand Up @@ -146,6 +145,7 @@
"eslint-plugin-react-hooks": "^4.6.2",
"globals": "^15.8.0",
"jsdom": "^24.1.0",
"mdast-util-lemmy-spoiler": "^2.0.0",
"prettier": "^3.3.2",
"pwa-asset-generator": "^6.3.1",
"release-it": "^17.4.1",
Expand Down
50 changes: 34 additions & 16 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/features/comment/CommentLinks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ export default function CommentLinks({ markdown }: CommentLinksProps) {

let links: LinkData[] = [];

visit(mdastTree, ["spoiler", "link", "image"], (node) => {
visit(mdastTree, ["details", "link", "image"], (node) => {
// don't show links within spoilers
if (node.type === "spoiler") return SKIP;
if (node.type === "details") return SKIP;

if (node.type === "link" || (!showCommentImages && node.type === "image"))
links.push({
Expand Down
4 changes: 2 additions & 2 deletions src/features/shared/markdown/InlineMarkdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useMemo } from "react";
import ReactMarkdown from "react-markdown";
import spoiler from "@aeharding/remark-lemmy-spoiler";
import superSub from "remark-supersub-lemmy";
import { spoilerSummaryOnlyRehype } from "./spoilerRehype";
import inlinifySpoiler from "./components/spoiler/inlinifySpoiler";

interface InlineMarkdownProps {
children: string;
Expand Down Expand Up @@ -49,7 +49,7 @@ export default function InlineMarkdown({ children }: InlineMarkdownProps) {
sub: "sub",
sup: "sup",
}}
remarkPlugins={[superSub, spoiler, spoilerSummaryOnlyRehype]}
remarkPlugins={[superSub, spoiler, inlinifySpoiler]}
>
{children}
</ReactMarkdown>
Expand Down
8 changes: 1 addition & 7 deletions src/features/shared/markdown/Markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import Table from "./components/Table";
import spoiler from "@aeharding/remark-lemmy-spoiler";
import Summary from "./components/spoiler/Summary";
import Details from "./components/spoiler/Details";
import spoilerRehype from "./spoilerRehype";
import { useMemo } from "react";
import rehypeHighlight from "rehype-highlight";

Expand Down Expand Up @@ -128,12 +127,7 @@ export default function Markdown({
[disableInternalLinkRouting, id, props.components],
)}
remarkPlugins={useMemo(
() => [
[customRemarkGfm, { connectedInstance }],
superSub,
spoiler,
spoilerRehype,
],
() => [[customRemarkGfm, { connectedInstance }], superSub, spoiler],
[connectedInstance],
)}
rehypePlugins={rehypePlugins}
Expand Down
7 changes: 6 additions & 1 deletion src/features/shared/markdown/components/spoiler/Details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ const HeaderItem = styled(IonItem)`
--background: none;
--background-hover: none;
strong {
// Differentiate from already bold title in header
font-weight: 900;
}
`;

const StyledIonAccordion = styled(IonAccordion)`
Expand Down Expand Up @@ -91,7 +96,7 @@ export default function Details({ children, node, id }: DetailsProps) {
<StyledIonAccordionGroup ref={accordionGroupRef} onIonChange={onChange}>
<StyledIonAccordion value="open">
<HeaderItem slot="header" onClick={(e) => e.stopPropagation()}>
{label}
<div>{label}</div>
</HeaderItem>
<div slot="content" className="collapse-md-margins">
{children}
Expand Down
19 changes: 19 additions & 0 deletions src/features/shared/markdown/components/spoiler/inlinifySpoiler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/// <reference types="mdast-util-lemmy-spoiler" />

import { visit } from "unist-util-visit";
import { Root } from "mdast";

/**
* Transform spoiler tree node to details/summary rehype html nodes
*/
export default function inlinifySpoiler() {
return function (tree: Root) {
visit(tree, "summary", function (node, _, parent) {
if (!parent) return;
if (parent.type !== "details") return;

parent.data = { hName: "p" };
parent.children = node.children;
});
};
}
62 changes: 0 additions & 62 deletions src/features/shared/markdown/spoilerRehype.ts

This file was deleted.

2 changes: 1 addition & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,6 @@ export default defineConfig({
setupFiles: "./src/setupTests.ts",
},
optimizeDeps: {
exclude: ["mdast-util-gfm-autolink-literal-lemmy"],
exclude: ["mdast-util-gfm-autolink-literal-lemmy", "remark-lemmy-spoiler"],
},
});

0 comments on commit be513d1

Please sign in to comment.