bun create astro@latest
bun install rehype-autolink-headings rehype-slug rehype-toc
astro.config.mjsimport remarkToc from 'remark-toc';
import rehypeAutolinkHeadings from 'rehype-autolink-headings';
import rehypeSlug from 'rehype-slug';
markdown: {
// ...
remarkPlugins: [
remarkReadingTime,
[remarkToc, { heading: 'contents', maxDepth: 3 }], // heading to inject toc, not case sensitive
],
rehypePlugins: [
[rehypeSlug, [rehypeAutolinkHeadings, { behavior: 'append' }]],
],
// ...
}
.md file, add ## Contents to inject table of contents.---
---
... ## Contents
<!-- ToC will render to:
<h2 id="contents">Contents</h2>
<ul>
<li>
<a href="#heading-1">Heading 1</a>
</li>
<li>
<a href="#heading-2">Heading 2</a>
</li>
</ul>
-->
...
References:
https://docs.astro.build/en/guides/markdown-content/#customizing-a-plugin
httpshttps://github.com/remarkjs/remark-toc?tab=readme-ov-file#what-is-this
Add package
bun astro add tailwind
Config tailwind.config.mjs
// disable default code element with `` in .md file
const disabledCss = {
'code::before': false,
'code::after': false,
code: false,
};
/** @type {import('tailwindcss').Config} */
export default {
darkMode: 'class',
content: ['./src/**/*.{astro,html,js,jsx,md,mdx,svelte,ts,tsx,vue}'],
theme: {
// add here to override tailwind default classes. Ex:
// fontFamily: {},
extend: {
// add here to extend tailwind classes
fontFamily:
{
atkinson: ['atkinson']
}
typography: {
DEFAULT: { css: disabledCss },
sm: { css: disabledCss },
lg: { css: disabledCss },
xl: { css: disabledCss },
'2xl': { css: disabledCss }
}
}
},
plugins: [require('@tailwindcss/typography')]
};
Config astro.config.mjs
export default defineConfig({
site: 'https://toantnm.pages.dev/',
integrations: [
...tailwind({
applyBaseStyles: false, // disable base.css applied default by astro
}),
],
markdown: {
remarkPlugins: [
remarkReadingTime,
[remarkToc, { heading: 'toc', maxDepth: 2 }],
],
// rehypePlugins: [rehypeAccessibleEmojis],
shikiConfig: {
themes: {
light: 'min-light',
dark: 'min-dark',
},
wrap: true,
},
},
});
Add tailwind.css (recommend in /src/assets/styles)
@tailwind base;
@tailwind components;
@tailwind utilities;
/* add web fonts*/
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap');
/* add local fonts*/
@font-face {
font-family: 'atkinson';
src:
url('/fonts/atkinson-regular.woff') format('woff'),
url('/fonts/atkinson-bold.woff') format('woff');
}
@layer base {
html.dark .astro-code,
html.dark .astro-code span {
color: var(--shiki-dark) !important;
background-color: var(--shiki-dark-bg) !important;
/* Optional, if you also want font styles */
font-style: var(--shiki-dark-font-style) !important;
font-weight: var(--shiki-dark-font-weight) !important;
text-decoration: var(--shiki-dark-text-decoration) !important;
}
}
/* style ` ` - compile to <code></code>
``` wil compile to <pre><code></code></pre> */
:not(pre) > code {
@apply text-orange-800 dark:text-orange-500 !important;
@apply bg-stone-400 font-semibold dark:bg-stone-800;
}
Install plugins:
bun install -d prettier-plugin-astro prettier-plugin-tailwindcss prettier-plugin-organize-imports
Config
Add to .prettierrc
{ "plugins": ["prettier-plugin-astro", "prettier-plugin-tailwindcss"] }
If using .prettierrc.mjs, add the following:
// .prettierrc.mjs
/** @type {import("prettier").Config} */
export default {
plugins: ['prettier-plugin-astro', 'prettier-plugin-tailwindcss'],
overrides: [
{
files: '*.astro',
options: {
parser: 'astro',
},
},
],
};
In VSCode, add the following to settings.json
"prettier.documentSelectors": ["**/*.astro"],
"[astro]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
Now VSCode should auto format (with auto sorting tailwindcss classes) the .astro file when save. If it’s not working, check Prettier extension ortry to reload/restart VSCode.
CLI command to format:
bunx prettier --write .
Reference:
https://marioyepes.com/blog/create-astro-starter-with-tailwind-and-pagefind/indexmd/
https://docs.astro.build/en/recipes/tailwind-rendered-markdown/#recipe
https://github.com/withastro/prettier-plugin-astro/blob/main/README.md#configuration
astro.config.mjsimport {
transformerNotationDiff,
// ...
} from '@shikijs/transformers'
...
export default defineConfig({
markdown: {
shikiConfig: {
themes: {
light: 'min-light',
dark: 'min-dark',
},
wrap: true,
},
})
...
.md filebun install -d @shikijs/transformers
import {
transformerMetaHighlight,
transformerMetaWordHighlight,
transformerNotationDiff,
transformerNotationErrorLevel,
transformerNotationFocus,
transformerNotationHighlight,
transformerNotationWordHighlight,
} from '@shikijs/transformers';
...
export default defineConfig({
markdown: {
shikiConfig: {
themes: {
light: 'min-light',
dark: 'min-dark',
},
wrap: true,
transformers: [
transformerNotationDiff(),
transformerNotationHighlight(),
transformerNotationWordHighlight(),
transformerNotationFocus(),
transformerNotationErrorLevel(),
transformerMetaHighlight(),
transformerMetaWordHighlight(),
],
},
},
});
...
Some notes:
With specificied theme, the generated HTML contains inline style for each token, so you don’t need extra CSS to style it. - https://shiki.style/guide/install#shorthands
Transformers only applies classes and does not come with styles; you can provide your own CSS rules to style them properly - https://shiki.style/packages/transformers#unstyled
Reference: