Compare commits

...

9 Commits

Author SHA1 Message Date
Mark Dumay
317eab54f8 Merge pull request #431 from gethinode/relref
Refactor link shortcode
2023-08-17 09:39:36 +02:00
mark
cb90ef363b Bump package version 2023-08-17 09:27:45 +02:00
Mark Dumay
f31309f863 Merge branch 'main' into relref 2023-08-17 09:24:22 +02:00
mark
a76a692075 Update build stats 2023-08-17 09:22:45 +02:00
mark
f52f758962 Refactor link shortcode 2023-08-17 09:22:39 +02:00
mark
bcc3a7877e Use proper page context to render inner content 2023-08-17 09:22:22 +02:00
mark
7b52da5ce0 Add link shortcode 2023-08-17 09:21:29 +02:00
mark
e7403bd1a4 Add multilanguage support 2023-08-17 09:21:09 +02:00
mark
2881eb4546 Trim trailing '/' to improve finding the right page 2023-08-17 09:20:53 +02:00
14 changed files with 107 additions and 42 deletions

View File

@@ -217,6 +217,31 @@ As an example, the following shortcode displays an image with rounded corners an
{{< /example >}} {{< /example >}}
<!-- markdownlint-enable MD037 --> <!-- markdownlint-enable MD037 -->
## Link
As an example, the following shortcodes render links in different formats.
<!-- markdownlint-disable MD037 -->
{{< example lang="hugo" >}}
- {{</* link hinode >}}Named link with default settings{{< /link */>}}
- {{</* link name=hinode cue=false tab=false >}}Named link opening in current tab w/o icon{{< /link */>}}
- {{</* link name=hinode cue=true tab=true >}}Named link opening in new tab with icon{{< /link */>}}
- {{</* link hinode /*/>}}
- {{</* link href="https://developer.mozilla.org" >}}External link{{< /link */>}}
- {{</* link "./projects/another-project" >}}Internal link with title{{< /link */>}}
- {{</* link url="projects/another-project" /*/>}}
- {{</* link url="/projects/another-project" /*/>}}
- {{</* link url="../projects/another-project" case=false /*/>}}
- {{</* link "/about/" /*/>}}
- {{</* link "/fr/about/" /*/>}}
- {{</* link "/fr/about" >}}About (French){{< /link */>}}
- {{</* link "#image" /*/>}}
- {{</* link "components/#map" /*/>}}
{{< /example >}}
<!-- markdownlint-enable MD037 -->
## Nav ## Nav
As an example, the following shortcode displays a tab group with vertically aligned pills. As an example, the following shortcode displays a tab group with vertically aligned pills.

View File

@@ -433,6 +433,7 @@
"icon", "icon",
"image", "image",
"leaflet-map-0", "leaflet-map-0",
"link",
"map", "map",
"nav", "nav",
"nav-0", "nav-0",

View File

@@ -149,8 +149,6 @@
"py-2", "py-2",
"py-5", "py-5",
"py-md-1", "py-md-1",
"ratio",
"ratio-16x9",
"rounded", "rounded",
"row", "row",
"row-cols-1", "row-cols-1",

View File

@@ -38,7 +38,21 @@
{{- $anchor = index $segments 1 -}} {{- $anchor = index $segments 1 -}}
{{ end }} {{ end }}
{{ end }} {{ end }}
{{- $destination = strings.TrimSuffix "/" $destination -}}
{{- $ref := $page.GetPage $destination -}} {{- $ref := $page.GetPage $destination -}}
{{- if not $ref }}
{{- $segments := split $destination "/" -}}
{{- if and (hasPrefix $destination "/") (gt (len $segments) 1) -}}
{{- $prefix := index $segments 1 -}}
{{- $page := .Page -}}
{{ with index (where site.Sites "Language.Lang" $prefix) 0 }}
{{- $path := printf "/%s" (strings.TrimPrefix (printf "/%s/" $prefix) $destination) }}
{{- $ref = .GetPage $path -}}
{{ end }}
{{- end -}}
{{- end -}}
{{- if not $ref -}} {{- if not $ref -}}
{{- errorf "partial [utilities/link.html] - Cannot find page: %s" $destination -}} {{- errorf "partial [utilities/link.html] - Cannot find page: %s" $destination -}}
{{- else -}} {{- else -}}

View File

@@ -50,6 +50,6 @@
</h2> </h2>
{{- end -}} {{- end -}}
<div id="{{ $parent }}-item-{{ $id }}" class="accordion-collapse collapse{{ if $show }} show{{ end }}{{ with $class }} {{ . }}{{ end }}" aria-labelledby="{{ $parent }}-heading-{{ $id }}" data-bs-parent="#{{ $parent }}"> <div id="{{ $parent }}-item-{{ $id }}" class="accordion-collapse collapse{{ if $show }} show{{ end }}{{ with $class }} {{ . }}{{ end }}" aria-labelledby="{{ $parent }}-heading-{{ $id }}" data-bs-parent="#{{ $parent }}">
<div class="accordion-body">{{ $body | markdownify | safeHTML }}</div> <div class="accordion-body">{{ $body | .Page.RenderString | safeHTML }}</div>
</div> </div>
</div> </div>

View File

@@ -26,7 +26,7 @@
{{- $body := .Inner -}} {{- $body := .Inner -}}
{{- if $open -}} {{- if $open -}}
{{- $pattern := printf "data-bs-parent=\"#%s\"" $id -}} {{- $pattern := printf "data-bs-parent=\"#%s\"" $id -}}
{{- $body = (replace .Inner $pattern "") | markdownify | safeHTML }} {{- $body = (replace .Inner $pattern "") | .Page.RenderString | safeHTML }}
{{- end -}} {{- end -}}
<div id="{{ $id }}" class="accordion mb-3{{ with $class }} {{ . }}{{ end }}"> <div id="{{ $id }}" class="accordion mb-3{{ with $class }} {{ . }}{{ end }}">

View File

@@ -55,7 +55,7 @@
<div class="d-flex alert alert-{{ $color }} {{ if $dismissible }}alert-dismissible fade show{{ end }}{{ with $class }} {{ . }}{{ end }}" role="alert"> <div class="d-flex alert alert-{{ $color }} {{ if $dismissible }}alert-dismissible fade show{{ end }}{{ with $class }} {{ . }}{{ end }}" role="alert">
{{ with $icon }}<div class="pt-1 pe-2">{{ . }}</div>{{ end }} {{ with $icon }}<div class="pt-1 pe-2">{{ . }}</div>{{ end }}
<div class="flex-grow-1 my-auto"> <div class="flex-grow-1 my-auto">
{{ trim .Inner " \r\n" | markdownify | safeHTML -}} {{ trim .Inner " \r\n" | .Page.RenderString | safeHTML -}}
{{ if $dismissible }}<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>{{ end }} {{ if $dismissible }}<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>{{ end }}
</div> </div>
</div> </div>

View File

@@ -4,10 +4,11 @@
named link or internal reference (in that order). Any inner text is rendered as the link title, otherwise it uses named link or internal reference (in that order). Any inner text is rendered as the link title, otherwise it uses
the host name (for external links), link title (for internal links), or anchor name (for any local references the host name (for external links), link title (for internal links), or anchor name (for any local references
containing a `#`). The shortcode supports the following named arguments: containing a `#`). The shortcode supports the following named arguments:
"name" Optional name of the link maintained in the "links" section of the site's parameters. If omitted, "href" Required reference to either an external link (if it starts with http), a named link (if it can be
the "url" argument should be provided instead. found in params.links), or internal reference. External and internal references may include an
"url" Optional url of the link, including the scheme ("http" or "https"). If omitted, the "name" argument anchor "#".
should be provided instead. "name" Alias of href.
"url" Alias of href.
"cue" Optional flag to indicate if an external link should show a visual cue, defaults to setting "cue" Optional flag to indicate if an external link should show a visual cue, defaults to setting
"main.externalLinks.cue" in the site's parameters. "main.externalLinks.cue" in the site's parameters.
"tab" Optional flag to indicate if an external link should open in a new tab, defaults to setting "tab" Optional flag to indicate if an external link should open in a new tab, defaults to setting
@@ -18,58 +19,82 @@
--> -->
{{- $error := false -}} {{- $error := false -}}
{{ $href := "" }}
{{ $name := "" }} {{ $name := "" }}
{{ $url := "" }} {{ $url := "" }}
{{ $class := "" }}
{{ $case := true }} {{ $case := true }}
{{ $cue := site.Params.main.externalLinks.cue }} {{ $cue := site.Params.main.externalLinks.cue }}
{{ $tab := site.Params.main.externalLinks.tab }} {{ $tab := site.Params.main.externalLinks.tab }}
{{ $text := trim .Inner " \r\n" | markdownify | safeHTML }} {{ $text := trim .Inner " \r\n" | .Page.RenderString | safeHTML }}
{{- $anchor := "" -}} {{- $anchor := "" -}}
{{ if .IsNamedParams }} {{ if .IsNamedParams }}
{{ $href = .Get "href" }}
{{ $name = .Get "name" }} {{ $name = .Get "name" }}
{{ $url = .Get "url" }} {{ $url = .Get "url" }}
{{ $cue = .Get "cue" | default site.Params.main.externalLinks.cue }} {{ $cue = .Get "cue" | default site.Params.main.externalLinks.cue }}
{{ $tab = .Get "tab" | default site.Params.main.externalLinks.tab }} {{ $tab = .Get "tab" | default site.Params.main.externalLinks.tab }}
{{ $case = .Get "case" | default true }} {{ $case = .Get "case" | default true }}
{{ else if strings.Contains (.Get 0) "/" }} {{- $class := .Get "class" -}}
{{ $url = .Get 0 }}
{{ else }} {{ else }}
{{ $name = .Get 0 }} {{ $href = .Get 0 }}
{{ end }} {{ end }}
{{ if not (or $name $url) }} {{- $href = or (or $href $name) $url -}}
{{ errorf "Expected param 'name' or 'url': %s" .Position -}} {{ if not $href }}
{{ errorf "Expected param 'href': %s" .Position -}}
{{ $error = true -}} {{ $error = true -}}
{{ end }} {{ end }}
{{ if $name }} {{- if hasPrefix $href "http" -}}
{{ $url = index site.Params.links $name }} {{ $url = $href }}
{{ if not $url }} {{- else if not (strings.Contains $href "/") -}}
{{ $url = $name }} {{ $url = index site.Params.links $href }}
{{- if strings.Contains $url "#" }} {{- end -}}
{{ $segments := split $url "#" }}
{{- if ne (len $segments) 2 }} {{ if not $url }}
{{ errorf "Malformed path, expected one anchor '#' only: '%s' at %s" $url .Position -}} {{- $href = strings.TrimPrefix "./" $href -}}
{{ else }} {{- if strings.Contains $href "#" }}
{{- $url = index $segments 0 -}} {{ $segments := split $href "#" }}
{{- $anchor = index $segments 1 -}} {{- if ne (len $segments) 2 }}
{{ if not $url }} {{ errorf "Malformed path, expected one anchor '#' only: '%s' at %s" $href .Position -}}
{{- $url = strings.TrimSuffix "/" .Page.RelPermalink -}} {{ else }}
{{- with .Page.Language }}{{ $url = strings.TrimPrefix (path.Join "/" .Lang) $url}}{{ end -}} {{- $url = index $segments 0 -}}
{{ end }} {{- $anchor = index $segments 1 -}}
{{ if not $url }}
{{- $url = strings.TrimSuffix "/" .Page.RelPermalink -}}
{{- with .Page.Language }}{{ $url = strings.TrimPrefix (path.Join "/" .Lang) $url}}{{ end -}}
{{ end }} {{ end }}
{{ end }} {{ end }}
{{ else }}
{{- $url = $href -}}
{{ end }} {{ end }}
{{ end }} {{ end }}
{{- $class := .Get "class" -}}
{{- $isExternal := ne (urls.Parse (absURL $url)).Host (urls.Parse site.BaseURL).Host -}} {{- $isExternal := ne (urls.Parse (absURL $url)).Host (urls.Parse site.BaseURL).Host -}}
{{- if not $isExternal -}} {{- if not $isExternal -}}
{{- $ref := .Page.GetPage $url -}} {{- $url = strings.TrimSuffix "/" $url -}}
{{- $ref := "" -}}
{{- if not $ref -}} {{- if not $ref -}}
{{- errorf "Cannot find page: '%s' at %s" $url .Position -}} {{- $ref = .Page.GetPage $url -}}
{{- end -}}
{{- if not $ref }}
{{- $segments := split $url "/" -}}
{{- if and (hasPrefix $url "/") (gt (len $segments) 1) -}}
{{- $prefix := index $segments 1 -}}
{{- $page := .Page -}}
{{ with index (where site.Sites "Language.Lang" $prefix) 0 }}
{{- $path := printf "/%s" (strings.TrimPrefix (printf "/%s/" $prefix) $url) }}
{{- $ref = .GetPage $path -}}
{{ end }}
{{- end -}}
{{- end -}}
{{- if not $ref -}}
{{- errorf "Cannot find page: '%s' at %s" $href .Position -}}
{{- $error = true -}} {{- $error = true -}}
{{- end -}} {{- end -}}
{{- end -}} {{- end -}}
@@ -81,3 +106,5 @@
{{ partial "utilities/link.html" (dict "destination" $url "text" $text "cue" $cue "tab" $tab "case" $case "class" $class "page" .Page) }} {{ partial "utilities/link.html" (dict "destination" $url "text" $text "cue" $cue "tab" $tab "case" $case "class" $class "page" .Page) }}
{{- end -}} {{- end -}}

View File

@@ -50,5 +50,5 @@
<div class="tab-pane{{ if $show }} active {{ if $fade}}show{{ end }}{{ end }} {{ if $fade}}fade{{ end }}" id="{{ $parent }}-{{ $id }}" <div class="tab-pane{{ if $show }} active {{ if $fade}}show{{ end }}{{ end }} {{ if $fade}}fade{{ end }}" id="{{ $parent }}-{{ $id }}"
role="tabpanel" aria-labelledby="{{ $parent }}-btn-{{ $id }}" tabindex="0" data-header="{{ $header }}" {{ if $show }}data-show-id="{{ $id }}"{{ end }} role="tabpanel" aria-labelledby="{{ $parent }}-btn-{{ $id }}" tabindex="0" data-header="{{ $header }}" {{ if $show }}data-show-id="{{ $id }}"{{ end }}
data-has-content="{{ gt (len $body) 0 }}" {{ if $disabled }} data-disabled-id="{{ $id }}"{{ end }}> data-has-content="{{ gt (len $body) 0 }}" {{ if $disabled }} data-disabled-id="{{ $id }}"{{ end }}>
{{ $body | markdownify | safeHTML }} {{ $body | .Page.RenderString | safeHTML }}
</div> </div>

View File

@@ -8,7 +8,7 @@
{{ $responsive = (slice "table-responsive") -}} {{ $responsive = (slice "table-responsive") -}}
{{- end -}} {{- end -}}
{{- $input := .Inner | markdownify }} {{- $input := .Inner | .Page.RenderString }}
{{- $input = replace $input "style=\"text-align:left\"" "class=\"text-start\"" -}} {{- $input = replace $input "style=\"text-align:left\"" "class=\"text-start\"" -}}
{{- $input = replace $input "style=\"text-align:center\"" "class=\"text-center\"" -}} {{- $input = replace $input "style=\"text-align:center\"" "class=\"text-center\"" -}}
{{- $input = replace $input "style=\"text-align:right\"" "class=\"text-end\"" -}} {{- $input = replace $input "style=\"text-align:right\"" "class=\"text-end\"" -}}

View File

@@ -79,7 +79,7 @@
{{ $datestr := (partial "utilities/date.html" (dict "date" $date "format" "long")) -}} {{ $datestr := (partial "utilities/date.html" (dict "date" $date "format" "long")) -}}
<p class="mb-0"><small class="text-body-secondary text-uppercase">{{ $datestr -}}</small></p> <p class="mb-0"><small class="text-body-secondary text-uppercase">{{ $datestr -}}</small></p>
{{ end }} {{ end }}
<p class="mt-3 mb-0">{{ $content | markdownify }}</p> <p class="mt-3 mb-0">{{ $content | .Page.RenderString }}</p>
</div> </div>
</div> </div>
</div> </div>

View File

@@ -13,7 +13,7 @@
{{ .Count }} {{ if gt .Count 1 }} {{ T "articles" }} {{ else }} {{ T "article" }} {{ end }} {{ .Count }} {{ if gt .Count 1 }} {{ T "articles" }} {{ else }} {{ T "article" }} {{ end }}
</div> </div>
<div class="col"> <div class="col">
<a href="{{ (path.Join .Page.RelPermalink) | relLangURL }}">{{ .Name | markdownify }}</a> <a href="{{ (path.Join .Page.RelPermalink) | relLangURL }}">{{ .Name | .Page.RenderString }}</a>
</div> </div>
</div> </div>
{{ end }} {{ end }}
@@ -34,7 +34,7 @@
<p class="text-body-secondary mt-5">{{ $year }}</p> <p class="text-body-secondary mt-5">{{ $year }}</p>
{{ $.Scratch.Set "lastYear" $year }} {{ $.Scratch.Set "lastYear" $year }}
{{ end }} {{ end }}
<a href="{{ (path.Join .Page.RelPermalink) | relLangURL }}">{{ if .Draft }}{{ T "draft" | upper }}: {{end}}{{ .Title | markdownify }}</a> <a href="{{ (path.Join .Page.RelPermalink) | relLangURL }}">{{ if .Draft }}{{ T "draft" | upper }}: {{end}}{{ .Title | .Page.RenderString }}</a>
</div> </div>
</div> </div>
{{ end }} {{ end }}

4
package-lock.json generated
View File

@@ -1,12 +1,12 @@
{ {
"name": "@gethinode/hinode", "name": "@gethinode/hinode",
"version": "0.18.3", "version": "0.18.4",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "@gethinode/hinode", "name": "@gethinode/hinode",
"version": "0.18.3", "version": "0.18.4",
"license": "MIT", "license": "MIT",
"devDependencies": { "devDependencies": {
"@fullhuman/postcss-purgecss": "^5.0.0", "@fullhuman/postcss-purgecss": "^5.0.0",

View File

@@ -1,6 +1,6 @@
{ {
"name": "@gethinode/hinode", "name": "@gethinode/hinode",
"version": "0.18.3", "version": "0.18.4",
"description": "Hinode is a clean documentation and blog theme for Hugo, an open-source static site generator", "description": "Hinode is a clean documentation and blog theme for Hugo, an open-source static site generator",
"keywords": [ "keywords": [
"hugo", "hugo",