Refactor mark shortcode

This commit is contained in:
mark
2023-12-27 09:20:29 +01:00
parent a5f5a9ff24
commit 24e96e2724
2 changed files with 42 additions and 14 deletions

27
data/structures/mark.yml Normal file
View File

@@ -0,0 +1,27 @@
comment: Highlights text by applying a background color.
arguments:
class:
type: string
optional: true
comment: Class attribute of the highlight element.
color:
type: select
optional: true
comment: >-
Theme color of the highlight. By default, the highlight uses the color of
the HTML mark function.
options:
values:
- primary
- secondary
- success
- danger
- warning
- info
- light
- dark
- white
- black
body:
optional: false
comment: Text to be marked.

View File

@@ -1,23 +1,24 @@
<!--
Highlights text by applying a background color. The shortcode supports the following arguments:
"color": Optional theme color of the highlight, either "primary", "secondary", "success", "danger",
"warning", "info", "light", "dark", "white" or "black". By default, the highlight uses the color of
the HTML mark function.
"class": Optional class attribute of the highlight element.
Copyright © 2023 The Hinode Team / Mark Dumay. All rights reserved.
Use of this source code is governed by The MIT License (MIT) that can be found in the LICENSE file.
Visit gethinode.com/license for more details.
-->
{{- $error := false -}}
{{ $color := .Get "color" | default "" -}}
{{ $supportedColors := slice "primary" "secondary" "success" "danger" "warning" "info" "light" "dark" -}}
{{ if and $color (not (in $supportedColors $color)) -}}
{{ errorf "Invalid value for param 'color': %s" .Position -}}
{{ $error = true -}}
{{ end -}}
{{ $error := false -}}
<!-- Validate arguments -->
{{ if partial "utilities/IsInvalidArgs.html" (dict "structure" "mark" "args" .Params) }}
{{ errorf "Invalid arguments: %s" .Position -}}
{{ $error = true }}
{{ end }}
<!-- Initialize arguments -->
{{ $color := .Get "color" | default "" -}}
{{- $class := .Get "class" | default "" -}}
{{- if $color }}{{ $class = printf "%s text-bg-%s" $class $color }}{{ end -}}
<!-- Main code -->
{{- if not $error -}}
{{- if $color }}{{ $class = printf "%s text-bg-%s" $class $color }}{{ end -}}
<mark{{ with $class }} class="{{ . }}"{{ end }}>{{ trim .Inner " \r\n" | .Page.RenderString -}}</mark>
{{- end -}}