Skip to content

Commit

Permalink
improve link replacement handling #13
Browse files Browse the repository at this point in the history
  • Loading branch information
Freymaurer committed Sep 17, 2024
1 parent 0b3c998 commit bf6973b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
6 changes: 6 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
25 changes: 21 additions & 4 deletions src/plugins/remark-replace-links.ts
Original file line number Diff line number Diff line change
@@ -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 = /^\{\{(.+?)\}\}$/;
Expand All @@ -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}`));
};
}
});
};
Expand Down
11 changes: 0 additions & 11 deletions src/statics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit bf6973b

Please sign in to comment.