-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: truncate html, hide images, add mask effect to post cards
- Loading branch information
1 parent
cda6c50
commit 2ce73b4
Showing
12 changed files
with
87 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
declare module "markdown-it-task-lists"; | ||
declare module "react-syntax-highlighter"; | ||
declare module "react-syntax-highlighter/dist/cjs/styles/prism"; | ||
declare module "rehype-truncate"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { remark } from "remark"; | ||
import remarkGfm from "remark-gfm"; | ||
import remarkParse from "remark-parse"; | ||
import remarkRehype from "remark-rehype"; | ||
import rehypeStringify from "rehype-stringify"; | ||
import rehypeTruncate from "rehype-truncate"; | ||
import rehypeCode from "rehype-highlight"; | ||
import rehypeRewrite from "rehype-rewrite"; | ||
|
||
// Parse markdown to HTML. (Post/Comment body) | ||
export async function markdownToHtml( | ||
markdown: string, | ||
rewriteLinks = true, | ||
truncate = true | ||
) { | ||
const result = await remark() | ||
.use(remarkParse) | ||
.use(remarkGfm) | ||
.use(remarkRehype) | ||
.use(rehypeCode) | ||
.use( | ||
// Truncate the HTML to reduce the size of the DOM and avoid | ||
// sending unnecessary HTML to the client. | ||
rehypeTruncate, | ||
truncate ? { maxChars: 200, ignoreTags: ["ul", "code", "pre"] } : false | ||
) | ||
.use(rehypeRewrite, { | ||
rewrite: (node) => { | ||
if (rewriteLinks) { | ||
// Rewrites any `<a>` to `<p>` to avoid any hydration or | ||
// validate DOM nesting errors. | ||
if (node.type === "element" && node.tagName === "a" && rewriteLinks) { | ||
node.tagName = "p"; | ||
} | ||
|
||
// Hide any images. | ||
if ( | ||
node.type === "element" && | ||
node.tagName === "img" && | ||
node.properties | ||
) { | ||
node.properties["width"] = "0"; | ||
node.properties["height"] = "0"; | ||
node.properties["style"] = "display: none; visibility: hidden;"; | ||
} | ||
} | ||
|
||
// Add aria-label to `<input type='checkbox'>` for better SEO. | ||
if ( | ||
node.type === "element" && | ||
node.tagName === "input" && | ||
node?.properties?.type === "checkbox" | ||
) { | ||
node.properties["aria-label"] = "Checkbox from checklist"; | ||
} | ||
}, | ||
}) | ||
.use(rehypeStringify) | ||
.process(markdown); | ||
return result.toString(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2ce73b4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
t3-blog – ./
t3-blog-leojuriolli.vercel.app
t3-blog-pi.vercel.app
t3-blog-git-main-leojuriolli.vercel.app