From 094dbe1c9a78ea20fd20307f242ce47a120864f0 Mon Sep 17 00:00:00 2001 From: hiiruki Date: Sun, 10 Sep 2023 20:50:01 +0700 Subject: [PATCH] blog: Hugo Open External Link in New Tab and Add Rel Attribute --- content/blog/hugo-link-render-hook/index.md | 81 +++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 content/blog/hugo-link-render-hook/index.md diff --git a/content/blog/hugo-link-render-hook/index.md b/content/blog/hugo-link-render-hook/index.md new file mode 100644 index 0000000..3038a86 --- /dev/null +++ b/content/blog/hugo-link-render-hook/index.md @@ -0,0 +1,81 @@ +--- +title: "Hugo Open External Link in New Tab and Add Rel Attribute" +description: "How to add a render hook for link in Hugo" +summary: "How to add a render hook for link in Hugo" +date: 2023-09-10T19:38:50+07:00 +draft: false +author: "Hiiruki" # ["Me", "You"] # multiple authors +tags: ["hugo", "render-hook", "goldmark"] +canonicalURL: "" +showToc: true +TocOpen: false +TocSide: 'right' # or 'left' +# weight: 1 +# aliases: ["/first"] +hidemeta: false +comments: false +disableHLJS: true # to disable highlightjs +disableShare: true +hideSummary: false +searchHidden: false +ShowReadingTime: true +ShowBreadCrumbs: true +ShowPostNavLinks: true +ShowWordCount: true +ShowRssButtonInSectionTermList: true +UseHugoToc: false +cover: + image: "" # image path/url + alt: "" # alt text + caption: "" # display caption under cover + relative: false # when using page bundles set this to true + hidden: true # only hide on current single page +# editPost: +# URL: "https://github.com/hiiruki/hiiruki.dev/tree/main/content/blog" +# Text: "Suggest Changes" # edit text +# appendFilePath: true # to append file path to Edit link +--- + +Hugo is using [goldmark](https://github.com/yuin/goldmark/) as its markdown renderer and has a [render hook](https://gohugo.io/templates/render-hooks/) feature. + +Previously, Hugo uses [Blackfriday](https://github.com/russross/blackfriday) as its markdown renderer in version below `v0.60.0`. Check the [changelog](https://github.com/gohugoio/hugo/releases/tag/v0.60.0) for more information. + +### Method 1 (No JavaScript) + +Make a file `layouts/_default/_markup/render-link.html` and add the following code: + +```html + + {{ .Text | safeHTML }} + +``` + +### Method 2 (JavaScript) + +Make a file `layouts/partials/extend_head.html` and add the following code: + +```html + +``` + +## References + +- https://gohugo.io/templates/render-hooks/ +- https://discourse.gohugo.io/t/open-external-links-in-new-tab-window/34000?page=2 +- https://agrimprasad.com/post/hugo-goldmark-markdown/ +- https://www.petanikode.com/hugo-render-hooks/