HackMD & Astro
이 콘텐츠는 아직 번역되지 않았습니다.
HackMD is a collaborative Markdown editor and publishing platform. You can use its API to manage your content in HackMD and display it in your Astro project.
Integrating with Astro
Section titled “Integrating with Astro”This guide uses the official @hackmd/api client to fetch your notes and markdown-it to render Markdown content.
Prerequisites
Section titled “Prerequisites”To get started, you will need:
- An Astro project - If you don’t have an Astro project yet, the installation guide will get you up and running.
- A HackMD account - You can sign up for free.
- A HackMD access token - Create one from the API section of your HackMD settings.
- At least one publicly readable note - Set the note’s read permission to Everyone so the example can safely publish it on your site.
Setting up credentials
Section titled “Setting up credentials”Create a .env file in the root of your project and add your HackMD access token:
HACKMD_API_ACCESS_TOKEN=<YOUR_ACCESS_TOKEN>Do not prefix this variable with PUBLIC_. This keeps the token available only to your server-side code and prevents Astro from exposing it to the browser.
.env files in Astro.
Installing dependencies
Section titled “Installing dependencies”Install the HackMD API client and Markdown renderer:
npm install @hackmd/api markdown-itpnpm add @hackmd/api markdown-ityarn add @hackmd/api markdown-itConfiguring HackMD
Section titled “Configuring HackMD”Create a hackmd.ts file in a new src/lib/ directory. This file initializes the API client, renders Markdown, and creates a URL-friendly identifier for each note:
import { API } from '@hackmd/api';import MarkdownIt from 'markdown-it';
export const client = new API(import.meta.env.HACKMD_API_ACCESS_TOKEN);
const md = new MarkdownIt({ html: false, linkify: true, typographer: true,});
export function renderMarkdown(content: string) { return md.render(content);}
export function getNoteSlug(note: { permalink: string | null; shortId: string }) { return note.permalink ?? note.shortId;}The html: false option prevents raw HTML in a note from being passed directly to your generated page. Standard Markdown is still rendered as HTML.
Your project will use the following files:
디렉터리src/
디렉터리lib/
- hackmd.ts
디렉터리pages/
- index.astro
디렉터리notes/
- [slug].astro
- .env
- astro.config.mjs
- package.json
Making a blog with Astro and HackMD
Section titled “Making a blog with Astro and HackMD”This example creates an index of publicly readable notes and a statically generated page for each note.
Displaying a list of notes
Section titled “Displaying a list of notes”Use getNoteList() in src/pages/index.astro to retrieve your notes. Filter the results so that only notes with the guest read permission are included in the public site:
---import { client, getNoteSlug } from '../lib/hackmd';
const notes = await client.getNoteList();const publicNotes = notes.filter((note) => note.readPermission === 'guest');---
<!doctype html><html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width" /> <title>Astro + HackMD</title> </head> <body> <main> <h1>My HackMD notes</h1> <ul> { publicNotes.map((note) => ( <li> <a href={`/notes/${getNoteSlug(note)}/`}>{note.title}</a> </li> )) } </ul> </main> </body></html>The access token can read private notes in your account. Keep the guest permission filter unless you intentionally want to include other notes in the generated site.
Generating note pages
Section titled “Generating note pages”Create src/pages/notes/[slug].astro to generate a static page for every public note. The note list provides the route and note ID, then getNote() retrieves the full Markdown content for that page:
---import { client, getNoteSlug, renderMarkdown } from '../../lib/hackmd';
export async function getStaticPaths() { const notes = await client.getNoteList();
return notes .filter((note) => note.readPermission === 'guest') .map((note) => ({ params: { slug: getNoteSlug(note) }, props: { noteId: note.id }, }));}
interface Props { noteId: string;}
const { noteId } = Astro.props;const note = await client.getNote(noteId);const content = renderMarkdown(note.content);---
<!doctype html><html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width" /> <title>{note.title}</title> </head> <body> <main> <article> <Fragment set:html={content} /> </article> </main> </body></html>Astro’s set:html directive inserts an HTML string without escaping it. This example first passes the note through markdown-it with raw HTML disabled. If you enable the html option for trusted authors, sanitize the rendered result before passing it to set:html.
Supporting more HackMD syntax
Section titled “Supporting more HackMD syntax”HackMD uses markdown-it with extensions for features such as task lists, footnotes, containers, and a table of contents. The minimal configuration above handles standard Markdown. Install only the markdown-it plugins required by your notes.
Publishing your site
Section titled “Publishing your site”To deploy your website, visit our deployment guides and follow the instructions for your preferred hosting provider.
If your project uses Astro’s default static mode, you must run a new build to publish changes made in HackMD. If your hosting provider supports it, you can use its webhook function to automatically trigger a new build when HackMD sends a webhook event.
Official Resources
Section titled “Official Resources”Community Resources
Section titled “Community Resources”daily-oops- A blog that uses HackMD as its CMSastro-hackmd- A minimal Astro site that uses HackMD as its CMS
더 많은 CMS 가이드
주요 CMS 파트너
-
CloudCannon
속도, 보안, 그리고 번거로움 없는 사용을 위해 만들어진 Git 기반 CMS입니다.