Skip to content

Commit

Permalink
feat: latex and umami analytics support (#162)
Browse files Browse the repository at this point in the history
* feat: added latex render support

* fix: eliminate warnings

* feat: support umami analytics

* fix: eliminate warnings

* pref: make latex support to be optional
  • Loading branch information
CuB3y0nd authored Oct 17, 2024
1 parent fde5c13 commit 0961f80
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 3 deletions.
19 changes: 16 additions & 3 deletions astro.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,36 @@ import { defineConfig } from 'astro/config'
import robotsTxt from 'astro-robots-txt'
import UnoCSS from 'unocss/astro'
import { themeConfig } from './src/.config'
import remarkMath from 'remark-math'
import rehypeKatex from 'rehype-katex'

// https://astro.build/config
export default defineConfig({
site: themeConfig.site.website,
prefetch: true,
base: '/',
markdown: {
remarkPlugins: [],
rehypePlugins: [],
remarkPlugins: [
remarkMath,
],
rehypePlugins: [
rehypeKatex,
],
shikiConfig: {
theme: 'dracula',
wrap: true,
},
},
integrations: [
UnoCSS({ injectReset: true }),
mdx(),
mdx({
remarkPlugins: [
remarkMath,
],
rehypePlugins: [
rehypeKatex,
],
}),
robotsTxt(),
sitemap(),
swup({
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
"astro-robots-txt": "^1.0.0",
"astro-seo": "^0.8.4",
"giscus": "^1.5.0",
"katex": "^0.16.11",
"rehype-katex": "^7.0.1",
"remark-math": "^6.0.0",
"typescript": "~5.5.4",
"vite": "^5.4.6"
},
Expand Down
100 changes: 100 additions & 0 deletions pnpm-lock.yaml

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

4 changes: 4 additions & 0 deletions src/.config/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,9 @@ export const defaultConfig: ThemeConfig = {
},
analytics: {
googleAnalyticsId: '',
umamiAnalyticsId: '',
},
latex: {
katex: false,
},
}
5 changes: 5 additions & 0 deletions src/components/Analytics.astro
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
---
import GoogleAnalytics from '~/components/analytics/GoogleAnalytics.astro'
import UmamiAnalytics from '~/components/analytics/UmamiAnalytics.astro'
import { themeConfig } from '~/.config'
const PUBLIC_GOOGLE_ANALYTICS_ID = import.meta.env.PUBLIC_GOOGLE_ANALYTICS_ID
const googleAnalyticsId = themeConfig.analytics.googleAnalyticsId || PUBLIC_GOOGLE_ANALYTICS_ID
const PUBLIC_UMAMI_ANALYTICS_ID = import.meta.env.PUBLIC_UMAMI_ANALYTICS_ID
const umamiAnalyticsId = themeConfig.analytics.umamiAnalyticsId || PUBLIC_UMAMI_ANALYTICS_ID
---

{googleAnalyticsId && <GoogleAnalytics id={googleAnalyticsId} />}
{umamiAnalyticsId && <UmamiAnalytics id={umamiAnalyticsId} />}
17 changes: 17 additions & 0 deletions src/components/LaTeX.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
import { themeConfig } from '~/.config'
const { latex } = themeConfig
const katex = latex.katex
---

{katex && (
<>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@latest/dist/katex.min.css" crossorigin="anonymous">
<script is:inline defer src="https://cdn.jsdelivr.net/npm/katex@latest/dist/katex.min.js" crossorigin="anonymous"></script>
<script is:inline defer src="https://cdn.jsdelivr.net/npm/katex@latest/dist/contrib/auto-render.min.js" crossorigin="anonymous"
onload="renderMathInElement(document.body);"></script>
</>
)}

10 changes: 10 additions & 0 deletions src/components/analytics/UmamiAnalytics.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
interface Props {
id: string
}
const { id } = Astro.props
---

<!-- Global site tag (script.js) - Umami Analytics -->
<script is:inline async defer src='https://analytics.umami.is/script.js' data-website-id={id}></script>
1 change: 1 addition & 0 deletions src/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ declare namespace App {

interface ImportMetaEnv {
readonly PUBLIC_GOOGLE_ANALYTICS_ID: string
readonly PUBLIC_UMAMI_ANALYTICS_ID: string
}

interface ImportMeta {
Expand Down
2 changes: 2 additions & 0 deletions src/layouts/LayoutDefault.astro
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import SiteFooter from '~/components/SiteFooter.astro'
import SiteNavigation from '~/components/SiteNavigation.astro'
import SiteTitle from '~/components/SiteTitle.astro'
import SiteSeo from '~/components/SiteSeo.astro'
import LaTeX from '~/components/LaTeX.astro'
import Analytics from '~/components/Analytics.astro'
import '~/styles/global.css'
Expand All @@ -14,6 +15,7 @@ const dark = themeConfig.appearance.theme === 'dark'
<html lang={lang} class:list={['animation-prepared', { dark }]}>
<head>
<slot name="seo"> <SiteSeo /> </slot>
<LaTeX />
<Analytics />
</head>

Expand Down
6 changes: 6 additions & 0 deletions src/types/themeConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface ThemeConfig {
comment: Partial<ConfigComment>
rss: ConfigRSS
analytics: ConfigAnalytics
latex: ConfigLaTeX
}

export type UserConfig = DeepPartial<ThemeConfig>
Expand Down Expand Up @@ -67,6 +68,11 @@ export interface ConfigRSS {
export interface ConfigAnalytics {
/** google analytics */
googleAnalyticsId: string
umamiAnalyticsId: string
}

export interface ConfigLaTeX {
katex: boolean
}

interface Colors {
Expand Down

0 comments on commit 0961f80

Please sign in to comment.