Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: post snippet #549

Merged
merged 1 commit into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions apps/blog/_posts/2015/04/tim-hieu-ve-giay-phep-gnu.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ thumbnail: https://4.bp.blogspot.com/-LlEOmpqG7Dg/VSi4YDNEClI/AAAAAAAACPU/VZ-xgK
slug: /2015/04/tim-hieu-ve-giay-phep-gnu.html
category: News
description: GNU (GNU General Public License) là giấy phép phần mềm tự do phổ biến nhất, ban đầu được thiết kê bới Richard Stallman, dành cho dự án GNU. Phiên bản 2 của giấy phép này được phát hành năm 1991, và phiên bản 3, phiên bản hiện tại, được phát hành năm 2007.
snippet: |
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-4044047400859099"
crossorigin="anonymous"></script>
<ins class="adsbygoogle"
style="display:block; text-align:center;"
data-ad-layout="in-article"
data-ad-format="fluid"
data-ad-client="ca-pub-4044047400859099"
data-ad-slot="4768236865"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
---

GNU (GNU General Public License) là giấy phép phần mềm tự do phổ biến nhất, ban đầu được thiết kê bới Richard Stallman, dành cho dự án GNU. Phiên bản 2 của giấy phép này được phát hành năm 1991, và phiên bản 3, phiên bản hiện tại, được phát hành năm 2007.
Expand Down
6 changes: 6 additions & 0 deletions apps/blog/app/[year]/[month]/[slug]/content/content.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
/* eslint-disable @typescript-eslint/no-unsafe-assignment -- have no idea to fix the line 37 */

import type { Post } from '@duyet/interfaces'
import { getPostBySlug } from '@duyet/libs/getPost'
import { markdownToHtml } from '@duyet/libs/markdownToHtml'
import { cn } from '@duyet/libs/utils'

import 'katex/dist/contrib/mhchem.min.js'
import 'katex/dist/katex.min.css'
import { Snippet } from './snippet'

export default function Content({ post }: { post: Post }) {
return (
Expand All @@ -30,6 +33,8 @@ export default function Content({ post }: { post: Post }) {
)}
dangerouslySetInnerHTML={{ __html: post.content || 'No content' }}
/>

<Snippet html={post.snippet || ''} />
</>
)
}
Expand All @@ -45,6 +50,7 @@ export async function getPost(slug: string[]) {
'category_slug',
'tags',
'series',
'snippet',
])
const content = await markdownToHtml(post.content || 'Error')

Expand Down
21 changes: 21 additions & 0 deletions apps/blog/app/[year]/[month]/[slug]/content/snippet.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { cn } from '@duyet/libs'

export function Snippet({
html,
className,
}: {
html: string
className?: string
}) {
if (!html) {
return null
}

return (
<div
className={cn(className)}
dangerouslySetInnerHTML={{ __html: html }}
suppressHydrationWarning
/>
)
}
1 change: 0 additions & 1 deletion apps/blog/next.redirects.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ const redirects = async () => [
destination: '/2015/04/mongo-web-query.html',
permanent: true,
},

]

module.exports = redirects
1 change: 1 addition & 0 deletions packages/interfaces/src/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type Post = {
excerpt?: string;
edit_url?: string;
series?: string;
snippet?: string;
[key: string]: any;
};

Expand Down
5 changes: 5 additions & 0 deletions packages/libs/getPost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export function getPostByPath(fullPath: string, fields: string[] = []): Post {
category_slug: 'unknown',
tags: [],
tags_slug: [],
snippet: '',
}

// Ensure only the minimal needed data is exposed
Expand Down Expand Up @@ -116,6 +117,10 @@ export function getPostByPath(fullPath: string, fields: string[] = []): Post {
data.description || content.split(' ').slice(0, 20).join(' ') + '...'
}

if (field === 'snippet') {
post['snippet'] = data.snippet || ''
}

if (typeof data[field] !== 'undefined') {
post[field] = data[field]
}
Expand Down
Loading