Skip to content

Commit

Permalink
markdownを特定の文字数まで減らす
Browse files Browse the repository at this point in the history
  • Loading branch information
tunamaguro committed May 5, 2023
1 parent 0b9f607 commit e843bc7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
13 changes: 13 additions & 0 deletions app/package-lock.json

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

2 changes: 2 additions & 0 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@
"react-dom": "^18.2.0",
"rehype-katex": "^6.0.2",
"remark-math": "^5.1.1",
"remove-markdown": "^0.5.0",
"satori": "^0.7.3",
"sharp": "^0.32.1",
"tailwindcss": "^3.2.7",
"twemoji-parser": "^14.0.0"
},
"devDependencies": {
"@tailwindcss/typography": "^0.5.9",
"@types/remove-markdown": "^0.3.1",
"@types/twemoji-parser": "^13.1.1",
"prettier": "^2.8.4",
"prettier-plugin-astro": "^0.8.0",
Expand Down
14 changes: 14 additions & 0 deletions app/src/server/getMarkdownExcerpt/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import removeMd from "remove-markdown";

export function getMarkdownExcerpt(markdown: string, maxExcerptLength = 70) {
let parsedText = removeMd(markdown);
// 空白を正規化
const contentText = parsedText.trim().replace(/\s+/g, " ");
const excerpt = contentText.slice(0, maxExcerptLength);

if (contentText.length > maxExcerptLength) {
return excerpt + "...";
}

return excerpt;
}

0 comments on commit e843bc7

Please sign in to comment.