I am building a component in Astro, which I want to fill with markdown from an Astro content collection. It all works, but I cannot get the markdown link from the md frontmatter to work as an html anchor tag in the component file.
This is the md frontmatter code:
---
[some md properties]
link: '[name](url) '
---
some markdown text
I first reference the different contents from the markdown file with template string literals and then try to replace the markdown in the anchor tag with some regex, but it only displays the md string as a working link:
import { getCollection } from 'astro:content';
const projects = await getCollection('projects');
console.log(projects);
---
{
projects.map((project) => (
<>
<div id='project_cards' class='bg-secondary rounded-[5px]'>
<div class='project_card flex-col bg-secondary'>
<img src={`${project.data.image}`} alt={`${project.data.title}`} />
</div>
<div class='project_card_description flex flex-col gap-[60px] p-[60px]'>
<SectionTitle sec1={`${project.data.title}`} sec2='' />
<p class='project_description'>{project.body}</p>
<div class='social_links'>
<a href={`${project.data.link}`}>{`${project.data.link}`}</a>
</div>
</div>
</div>
</>
))
}
<script>
//md links to html anchor tags
const md_social_link = `${project.data.link}`;
// md replacement for anchor tags
const html_social_link = md_social_link.replace(
/[([^[]+)]((([^)]*)))/gim,
'<a href="$3" target?"_blank">$1</a>'
/**
* '<a href="$3">$1</a>': This is the replacement string for the matched pattern. It's HTML code that will be used to replace the Markdown-style link.
*
* <a href="$3">$1</a>: This is an anchor tag (<a>). $3 and $1 are placeholders that refer to the capturing groups in the regex pattern:
* $3: This refers to the URL captured by the second capturing group ((([^)]*)))).
* $1: This refers to the link text captured by the first capturing group ([^[]+).s
**/
);
//append to div ".social_links"
console.log(html_social_link);
let div_social_links = document.querySelector('.social_links');
// Check if div_social_links is not null before using it
if (div_social_links) {
div_social_links.innerHTML = html_social_link;
} else {
// Handle the case where .social_links is not found
console.error('Error: .social_links element not found.');
}
</script>
In the script part, I get the following error when hovering on the template string: Cannot find name "project". I suppose that there must be a problem with correctly referencing the frontmatter property, but I don’t know how I can fix it to work with a markdown link.
How can I achieve to effectively split the markdown link so that I can use it for the anchor tag? Is this even possible?


