truncate markdown to 150 char
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
417ad32fba
commit
2d27055bf8
17
lib/news.ts
17
lib/news.ts
|
@ -3,6 +3,7 @@ import path from "path";
|
|||
|
||||
import { parse } from "date-fns";
|
||||
import matter from "gray-matter";
|
||||
import truncateMarkdown from "markdown-truncate";
|
||||
import { MDXRemoteSerializeResult } from "next-mdx-remote";
|
||||
import { serialize } from "next-mdx-remote/serialize";
|
||||
|
||||
|
@ -66,7 +67,8 @@ export async function getNewsByTerm(
|
|||
export async function getNewsBySlug(
|
||||
year: string,
|
||||
term: Term,
|
||||
slug: string
|
||||
slug: string,
|
||||
shortened = false
|
||||
): Promise<News> {
|
||||
const raw = await fs.readFile(
|
||||
path.join(NEWS_PATH, year, term, `${slug}.md`),
|
||||
|
@ -74,9 +76,16 @@ export async function getNewsBySlug(
|
|||
);
|
||||
const { content, data: metadata } = matter(raw);
|
||||
const slugDate = slug.split("-").slice(0, 3).join("-");
|
||||
let truncatedContent: string = content;
|
||||
if (shortened) {
|
||||
truncatedContent = truncateMarkdown(content, {
|
||||
limit: 150,
|
||||
ellipsis: true,
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
content: await serialize(content),
|
||||
content: await serialize(truncatedContent),
|
||||
metadata: {
|
||||
...metadata,
|
||||
date: getLocalDateFromEST(
|
||||
|
@ -103,7 +112,9 @@ export async function getRecentNews(): Promise<News[]> {
|
|||
try {
|
||||
const newsInTerm = await getNewsByTerm(year, TERMS[term]);
|
||||
return await Promise.all(
|
||||
newsInTerm.map((slug) => getNewsBySlug(year, TERMS[term], slug))
|
||||
newsInTerm.map((slug) => {
|
||||
return getNewsBySlug(year, TERMS[term], slug, true);
|
||||
})
|
||||
);
|
||||
} catch (error) {
|
||||
return [];
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
"fs-extra": "^10.0.0",
|
||||
"image-size": "^1.0.0",
|
||||
"ldapts": "^3.1.0",
|
||||
"markdown-truncate": "^1.0.4",
|
||||
"next": "11.0.1",
|
||||
"next-mdx-remote": "3.0.4",
|
||||
"prettier": "^2.3.0",
|
||||
|
@ -4719,6 +4720,11 @@
|
|||
"url": "https://github.com/sponsors/wooorm"
|
||||
}
|
||||
},
|
||||
"node_modules/markdown-truncate": {
|
||||
"version": "1.0.4",
|
||||
"resolved": "https://registry.npmjs.org/markdown-truncate/-/markdown-truncate-1.0.4.tgz",
|
||||
"integrity": "sha512-sojm7PWqbgIfUoSVyKyyUN3glbwEgfXqL75HYvGjBHQuCkNaEHglyYt3biEIZG81H/CxhTtf2DEu4tLGWoK65Q=="
|
||||
},
|
||||
"node_modules/md5.js": {
|
||||
"version": "1.3.5",
|
||||
"resolved": "https://registry.npmjs.org/md5.js/-/md5.js-1.3.5.tgz",
|
||||
|
@ -11576,6 +11582,11 @@
|
|||
"repeat-string": "^1.0.0"
|
||||
}
|
||||
},
|
||||
"markdown-truncate": {
|
||||
"version": "1.0.4",
|
||||
"resolved": "https://registry.npmjs.org/markdown-truncate/-/markdown-truncate-1.0.4.tgz",
|
||||
"integrity": "sha512-sojm7PWqbgIfUoSVyKyyUN3glbwEgfXqL75HYvGjBHQuCkNaEHglyYt3biEIZG81H/CxhTtf2DEu4tLGWoK65Q=="
|
||||
},
|
||||
"md5.js": {
|
||||
"version": "1.3.5",
|
||||
"resolved": "https://registry.npmjs.org/md5.js/-/md5.js-1.3.5.tgz",
|
||||
|
|
|
@ -24,9 +24,10 @@
|
|||
"@squoosh/lib": "^0.4.0",
|
||||
"date-fns": "^2.11.1",
|
||||
"date-fns-tz": "^1.1.6",
|
||||
"ldapts": "^3.1.0",
|
||||
"fs-extra": "^10.0.0",
|
||||
"image-size": "^1.0.0",
|
||||
"ldapts": "^3.1.0",
|
||||
"markdown-truncate": "^1.0.4",
|
||||
"next": "11.0.1",
|
||||
"next-mdx-remote": "3.0.4",
|
||||
"prettier": "^2.3.0",
|
||||
|
|
|
@ -16,3 +16,10 @@ declare module "*.md" {
|
|||
|
||||
export default ReactComponent;
|
||||
}
|
||||
|
||||
declare module "markdown-truncate" {
|
||||
export default function truncateMarkdown(
|
||||
inputText: string,
|
||||
options: { limit: number; ellipsis: boolean }
|
||||
): string;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue