content icon indicating copy to clipboard operation
content copied to clipboard

Trying to make binding-data-in-markdown work with markdown links

Open xalunda opened this issue 1 year ago • 8 comments

Environment

  • Operating System: Darwin
  • Node Version: v18.20.4
  • Nuxt Version: 3.13.1
  • CLI Version: 3.13.1
  • Nitro Version: 2.9.7
  • Package Manager: [email protected]
  • Builder: -
  • User Config: compatibilityDate, devtools, future, extends, alias, i18n, css, vite
  • Runtime Modules: -
  • Build Modules: -

Reproduction

https://stackblitz.com/edit/nuxt-starter-ysynij?file=content%2Findex.md

Describe the bug

Hi everyone!

I'm not sure if this is a bug or a misuse on my part.

I'm trying to make a simple link in markdown containing binding-data-in-markdown

[text](url)

I have two variables websiteName and websiteLink in the :data prop of ContentRenderer.

I'm tried lots of syntax combinations, I'm trying to:

[{{$doc.websiteName}}]({{$doc.websiteLink}})

The text part is correctly replaced but the link part is not:

%7B%7B$doc.websiteLink%7D%7D

Here is a reproduction.

Let me know if I can help in any way.

Additional context

I also tried to override the ProseA component to check for debug. By the time it reaches this component, the href prop is already this one:

%7B%7B$doc.websiteLink%7D%7D

Logs

No response

xalunda avatar Sep 21 '24 08:09 xalunda

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Nov 21 '24 02:11 github-actions[bot]

Hello @xalunda. Have you found a solution?

spyesx avatar Dec 13 '24 17:12 spyesx

@spyesx I did not, unfortunately...

xalunda avatar Dec 13 '24 18:12 xalunda

@xalunda Thx for the reply.

I can believe we're the only ones with this issue. Maybe I'm doing something wrong. Does a dev have an recommendation?

spyesx avatar Dec 13 '24 18:12 spyesx

We might be doing something "not meant to do". My "sanity check" answer to that.

I also tried to post on Reddit, no answers.

It'd be great if someone from the team could take a look.

xalunda avatar Dec 13 '24 18:12 xalunda

The team has already provided an answer to the problem! https://github.com/nuxt/content/issues/2637

HugoRCD avatar Dec 19 '24 19:12 HugoRCD

But I agree it would be a great feature!

HugoRCD avatar Dec 19 '24 19:12 HugoRCD

I found a simple way using hooks that should work in v3 aswell. Here is a snippet:

v2:

// server/plugins/content.ts
export default defineNitroPlugin((nitroApp) => {
    nitroApp.hooks.hook('content:file:beforeParse', (file: { _id: string; body: string; }) => {
        const data: Data = {
            name: 'Alice',
            age: 30
        };
        if (file._id.endsWith('.md')) {
            file.body = file.body.replace(/{{\s*\$data\.(\w+)\s*(?:\|\|\s*'([^']*)'\s*)?}}/g, (_, variable, defaultValue) =>
                appConfig.shadcnDocs?.data?.[variable] ?? defaultValue ?? ''
            );
        }
    })
})

v3 (no tested):

export default defineNuxtConfig({
  hooks: {
    'content:file:beforeParse' ( {file} ) {
        const data: Data = {
            name: 'Alice',
            age: 30
        };
        if (file._id.endsWith('.md')) {
            file.body = file.body.replace(/{{\s*\$data\.(\w+)\s*(?:\|\|\s*'([^']*)'\s*)?}}/g, (_, variable, defaultValue) =>
                appConfig.shadcnDocs?.data?.[variable] ?? defaultValue ?? ''
            );
        }
    }
  }
})

usage:

Hello, {{ $data.name }}! You are {{ $data.age || 'unknown age' }} years old. 

Your country is {{ $data.country || 'unknown' }}.

brospars avatar Feb 28 '25 10:02 brospars