From 9f2b67c08ca91d378146711eb865b3455bb8fea0 Mon Sep 17 00:00:00 2001 From: Xin Date: Mon, 1 Apr 2024 16:55:11 +0200 Subject: [PATCH] feat: pdf shortcode (unstable) (#345) --- layouts/partials/utils/file-path.html | 21 +++++++++++++++++++++ layouts/shortcodes/pdf.html | 9 +++++++++ 2 files changed, 30 insertions(+) create mode 100644 layouts/partials/utils/file-path.html create mode 100644 layouts/shortcodes/pdf.html diff --git a/layouts/partials/utils/file-path.html b/layouts/partials/utils/file-path.html new file mode 100644 index 00000000..17945517 --- /dev/null +++ b/layouts/partials/utils/file-path.html @@ -0,0 +1,21 @@ +{{/* This utility is used to get the file path from absolute, relative path or URL. */}} + +{{- $path := .path -}} +{{- $page := .page -}} + +{{- $isLocal := not (urls.Parse $path).Scheme -}} +{{- $isPage := and (eq $page.Kind "page") (not $page.BundleType) -}} +{{- $startsWithSlash := hasPrefix $path "/" -}} +{{- $startsWithRelative := hasPrefix $path "../" -}} + +{{- if and $path $isLocal -}} + {{- if $startsWithSlash -}} + {{/* File under static directory */}} + {{- $path = (relURL (strings.TrimPrefix "/" $path)) -}} + {{- else if and $isPage (not $startsWithRelative) -}} + {{/* File is a sibling to the individual page file */}} + {{ $path = (printf "../%s" $path) }} + {{- end -}} +{{- end -}} + +{{- return $path -}} diff --git a/layouts/shortcodes/pdf.html b/layouts/shortcodes/pdf.html new file mode 100644 index 00000000..9e3c4a93 --- /dev/null +++ b/layouts/shortcodes/pdf.html @@ -0,0 +1,9 @@ +{{/* Shortcode to include a PDF file in a page. */}} + +{{- $path := .Get 0 -}} +{{- $url := partial "utils/file-path" (dict "page" .Page "path" $path) -}} + + +
+ +