-
-
Notifications
You must be signed in to change notification settings - Fork 643
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Icon for external links #398
Comments
You could do this pretty easily right now by using a custom Vue component instead of the markdown link, but I agree, that would be a cool addition. You would want to make it able to be turned on and off as typically it's only used for external links as you said. Alternatively you could make the link interpreter smart so that when it detects an external link in the markdown it adds the icon. |
Did some digging through Vuepress and how Nuxt Content works, so here are the general findings I have which may help to make it easier to add this feature. First, Vuepress has a markdown-it plugin that checks links whether they are external links using the regex: The same plugin then later on adds the We can see the OutBoundLink file here and find its a functional component showing the SVG for the external link icon. A check on the official Vuepress confirms the correct SVG as seen below: With that said, seems like Nuxt Content uses content/packages/content/lib/utils.js Lines 13 to 19 in 26b20e4
I believe we can add the logic for inserting the icon in this plugin. The question now would be whether to replace Sorry if this ended up being long winded than necessary. Just hope I can help to facilitate this particular feature :) |
GREAT detective work!!! |
@davydnorris thank you! Somehow I was able to find a way to achieve this WITH export default {
content: {
markdown: {
// https://github.com/remarkjs/remark-external-links#options
remarkExternalLinks: {
target: '_self',
rel: 'nofollow'
}
}
}
} The commented line piqued my interest on what are the other options we have, and apparently it has the I've also noticed that the current theme already has an icon for external link, albeit only used for It is the IconExternalLink.vue component in the default Nuxt Content Docs theme here: content/packages/theme-docs/src/components/global/icons/IconExternalLink.vue Lines 1 to 12 in 26b20e4
With all the pieces of the puzzle, I was able to make it work with the following single step. Add External Link IconAdd the content: {
markdown: {
remarkExternalLinks: {
content: {
type: "element",
tagName: "svg",
properties: {
className: ["w-4", "h-4", "ml-1", "-mt-1", "stroke-2"],
style: ["stroke-linecap: round;", "stroke-linejoin: round;"],
fill: "none",
viewBox: "0 0 24 24",
stroke: "currentColor",
},
children: [
{
type: "element",
tagName: "path",
properties: {
d:
"M10 6H6a2 2 0 00-2 2v10a2 2 0 002 2h10a2 2 0 002-2v-4M14 4h6m0 0v6m0-6L10 14",
},
},
],
},
contentProperties: {
className: [
"inline-block",
"align-middle",
"text-gray-600",
"dark:text-gray-400",
],
},
},
},
}, The above SVG is basically taken from the IconExternalLink component with minor adjustments, such as Screenshots
CodeSandbox Linkhttps://codesandbox.io/s/distracted-hypatia-z2p7b?file=/nuxt.config.js Might seem a little hacky, but on the other hand we are using the existing options provided by the EDIT: Refer my follow up comment below for a simplified version of this approach (Link) |
I don't know when the content is parsed vs. when it is rendered, but you can insert normal Vue components directly into your markdown, so I wonder if you couldn't just save IconExternalLink.vue as your own global component, including any styling, and then specify the Vue component in options.content:
|
Just tried it out and you are absolutely correct! The best thing is we can just use We can then simplify the content: {
markdown: {
remarkExternalLinks: {
content: {
type: "element",
tagName: "icon-external-link",
properties: {
className: ["w-4", "h-4", "ml-1", "-mt-1"],
},
},
contentProperties: {
className: ["inline-block", "align-middle", "text-gray-600", "dark:text-gray-400"],
},
},
},
}, Still had to include the classes for styling since the |
Hey. Sorry guys to not let you know where I was about that. I'm currently in hollydays, cycling in mountains. I actually came to the exact same way of doing last week before leaving, and was planing to make a PR at my return, at the end of the week 😅. I however used a .link.link-outbound class on the element to let users style it as they want. Thanks anyway for the work, I hope you at least had fun 😛. Matt' |
@azrikahar - that's awesome! If you did make your own component, you could also include a lot, if not all, of the styling in the .vue file itself, but this is a great outcome. I actually use Vuetify in all my projects so I may end up rolling my own component and using the Material Design icons and Vuetify styles, since I have added the default styling into the content (see #379) |
Hi,
Does Nuxt content provide a way to show a icon after each external link, like Vuepress does?
Exemple:
Thanks.
Mathieu.
The text was updated successfully, but these errors were encountered: