From c3bcaa74899e2436230a62e60eb11826db95d073 Mon Sep 17 00:00:00 2001 From: Kamran Ahmed Date: Tue, 30 Apr 2024 11:18:41 +0100 Subject: [PATCH] Add shortcode support --- src/components/Guide/GuideContent.astro | 22 +++++++++++++--------- src/components/GuideHeader.astro | 7 ++++--- src/components/GuideListItem.astro | 3 ++- src/data/guides/backend-developer-tools.md | 4 ++-- src/data/guides/backend-languages.md | 6 +++--- src/lib/markdown.ts | 15 +++++++++++++++ src/pages/backend/developer-skills.astro | 5 +++-- src/pages/backend/developer-tools.astro | 5 +++-- src/pages/backend/languages.astro | 5 +++-- src/pages/guides/[guideId].astro | 5 +++-- 10 files changed, 51 insertions(+), 26 deletions(-) diff --git a/src/components/Guide/GuideContent.astro b/src/components/Guide/GuideContent.astro index fbfbfdbc7..3eeb4db1e 100644 --- a/src/components/Guide/GuideContent.astro +++ b/src/components/Guide/GuideContent.astro @@ -2,6 +2,7 @@ import { getGuideTableOfContent, type GuideFileType } from '../../lib/guide'; import MarkdownFile from '../MarkdownFile.astro'; import { TableOfContent } from '../TableOfContent/TableOfContent'; +import { replaceVariables } from '../../lib/markdown'; interface Props { guide: GuideFileType; @@ -26,18 +27,21 @@ const { frontmatter: guideFrontmatter, author } = guide; }
-

{guideFrontmatter.title}

-

+

+ {replaceVariables(guideFrontmatter.title)} +

+

{author.frontmatter.name}

-

- {frontmatter.title} +

+ {replaceVariables(frontmatter.title)}

diff --git a/src/components/GuideListItem.astro b/src/components/GuideListItem.astro index 118c66dfb..ae9d778be 100644 --- a/src/components/GuideListItem.astro +++ b/src/components/GuideListItem.astro @@ -1,5 +1,6 @@ --- import type { GuideFileType } from '../lib/guide'; +import { replaceVariables } from '../lib/markdown'; export interface Props { guide: GuideFileType; @@ -20,7 +21,7 @@ const { frontmatter, id } = guide; - {frontmatter.title} + {replaceVariables(frontmatter.title)} { frontmatter.isNew && ( diff --git a/src/data/guides/backend-developer-tools.md b/src/data/guides/backend-developer-tools.md index acf2414b6..5e09d1485 100644 --- a/src/data/guides/backend-developer-tools.md +++ b/src/data/guides/backend-developer-tools.md @@ -1,10 +1,10 @@ --- -title: '25 Essential Backend Development Tools for 2024' +title: '25 Essential Backend Development Tools for @currentYear@' description: 'Elevate your development process with these 25 essential backend developer tools.' authorId: fernando excludedBySlug: '/backend/developer-tools' seo: - title: '25 Essential Backend Development Tools for 2024' + title: '25 Essential Backend Development Tools for @currentYear@' description: 'Elevate your coding with backend developer tools that bring efficiency, scalability, and innovation to your projects. Improve your development process today!' isNew: true type: 'textual' diff --git a/src/data/guides/backend-languages.md b/src/data/guides/backend-languages.md index 88406ae47..1ff7fd17e 100644 --- a/src/data/guides/backend-languages.md +++ b/src/data/guides/backend-languages.md @@ -1,10 +1,10 @@ --- -title: 'The 5 Best Backend Development Languages to Master (2024)' -description: 'Discover the best backend development languages to master in 2024.' +title: 'The 5 Best Backend Development Languages to Master (@currentYear@)' +description: 'Discover the best backend development languages to master in @currentYear@.' authorId: fernando excludedBySlug: '/backend/languages' seo: - title: 'The 5 Best Backend Development Languages to Master (2024)' + title: 'The 5 Best Backend Development Languages to Master (@currentYear@)' description: 'Discover the best backend development languages to learn right now for career development, with practical tips from an experienced developer.' isNew: false type: 'textual' diff --git a/src/lib/markdown.ts b/src/lib/markdown.ts index b7114f012..af0a1c2ae 100644 --- a/src/lib/markdown.ts +++ b/src/lib/markdown.ts @@ -1,6 +1,21 @@ // @ts-ignore import MarkdownIt from 'markdown-it'; +// replaces @variableName@ with the value of the variable +export function replaceVariables( + markdown: string, + variables: Record = {}, +): string { + const allVariables: Record = { + ...variables, + currentYear: new Date().getFullYear().toString(), + }; + + return markdown.replace(/@([^@]+)@/g, (match, p1) => { + return allVariables[p1] || match; + }); +} + export function markdownToHtml(markdown: string, isInline = true): string { try { const md = new MarkdownIt({ diff --git a/src/pages/backend/developer-skills.astro b/src/pages/backend/developer-skills.astro index 85325741b..4d1fff5c1 100644 --- a/src/pages/backend/developer-skills.astro +++ b/src/pages/backend/developer-skills.astro @@ -4,6 +4,7 @@ import GuideHeader from '../../components/GuideHeader.astro'; import BaseLayout from '../../layouts/BaseLayout.astro'; import { getGuideById } from '../../lib/guide'; import { getOpenGraphImageUrl } from '../../lib/open-graph'; +import {replaceVariables} from "../../lib/markdown"; const guideId = 'backend-developer-skills'; const guide = await getGuideById(guideId); @@ -17,8 +18,8 @@ const ogImageUrl = getOpenGraphImageUrl({ --- null); @@ -20,8 +21,8 @@ const ogImageUrl = getOpenGraphImageUrl({ ---