From bf6973bb59233777965903e0ce6a4fdb0b22bf5b Mon Sep 17 00:00:00 2001 From: Kevin F Date: Tue, 17 Sep 2024 14:04:47 +0200 Subject: [PATCH] improve link replacement handling #13 --- CONTRIBUTING.md | 6 ++++++ src/plugins/remark-replace-links.ts | 25 +++++++++++++++++++++---- src/statics.ts | 11 ----------- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index b09a47a..8bec6db 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,3 +1,9 @@ +# URLS + +This project uses a link collection in `src/statics.ts` to manage all URLS. These MUST be imported in .astro components and can be used in markdown files via `[Back home]({{internal_home}})`. Pay attention to the double curly braces (`{{}}`) around the keyword from `src/statics.ts`. This should **not** be case sensitive. Should you add a typo or the link in the url collection gets updated the plugin `src/plugins/remark-replace-links.ts` will create a warning in the console. + +# ASTRO + This repo is based on the default [astro.build](https://astro.build)! See below for more information. ## Astro Starter Kit: Basics diff --git a/src/plugins/remark-replace-links.ts b/src/plugins/remark-replace-links.ts index 3a09969..949b968 100644 --- a/src/plugins/remark-replace-links.ts +++ b/src/plugins/remark-replace-links.ts @@ -1,9 +1,24 @@ -// src/plugins/remark-replace-links.js -import { getUrlFromEnum } from '../statics'; +import chalk from 'chalk'; +import { URLS } from '../statics'; import type { Root } from 'mdast'; import type { Plugin, Transformer } from 'unified'; import {visit} from 'unist-util-visit' +// ANSI escape codes for color +const ANSI_RESET = '\x1b[0m'; +const ANSI_YELLOW = '\x1b[33m'; + +function getUrlFromEnum(key: string): string | null { + // Check if the input key is a valid key in the URLS enum + if (key in URLS) { + // Return the associated value from the enum + return URLS[key as keyof typeof URLS]; + } else { + // Return undefined if the key is not found + return null; + } +} + function extractTextInsideDoubleCurlyBraces(input: string) { // Regular expression to match strings starting with '{{' and ending with '}}' const regex = /^\{\{(.+?)\}\}$/; @@ -22,11 +37,13 @@ export function remarkReplaceLinks(): Plugin<[], Root> { visit(tree, 'link', (node) => { const key = extractTextInsideDoubleCurlyBraces(node.url); if (key) { - let replacedValue = getUrlFromEnum(key) + const replacedValue = getUrlFromEnum(key.toUpperCase()) if (replacedValue) { // console.log(node) node.url = replacedValue; - } + } else { + console.log(chalk.yellow(`\t - ⚠️ No URL found for key: "${key}" in file: ${file.path}`)); + }; } }); }; diff --git a/src/statics.ts b/src/statics.ts index 4b616b3..2b9a007 100644 --- a/src/statics.ts +++ b/src/statics.ts @@ -18,15 +18,4 @@ export enum URLS { GITHUB_REPO = "https://github.com/nfdi4plants/arc-website" -} - -export function getUrlFromEnum(key: string): string | null { - // Check if the input key is a valid key in the URLS enum - if (key in URLS) { - // Return the associated value from the enum - return URLS[key as keyof typeof URLS]; - } else { - // Return undefined if the key is not found - return null; - } } \ No newline at end of file