From 2b001f07615926eec5261d3616227214020733e7 Mon Sep 17 00:00:00 2001 From: jason810496 <810496@email.wlsh.tyc.edu.tw> Date: Fri, 21 Jun 2024 23:48:13 +0800 Subject: [PATCH] feat: add startLine and endLine support for codeimporter --- layouts/shortcodes/codeimporter.html | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/layouts/shortcodes/codeimporter.html b/layouts/shortcodes/codeimporter.html index 493f4448..d461b5bb 100644 --- a/layouts/shortcodes/codeimporter.html +++ b/layouts/shortcodes/codeimporter.html @@ -1,8 +1,27 @@ {{ $url := .Get "url" }} {{ $type := .Get "type" }} -{{ with resources.GetRemote (urls.Parse $url) }} -{{ $codeBlock := printf "```%s\n%s\n```" $type .Content }} -{{ $codeBlock | markdownify }} +{{ $startLine := .Get "startLine" | default 1 | int }} +{{ $startLine = sub $startLine 1 }} +{{ $endLine := .Get "endLine" | default -1 | int }} +{{ $selectedLines := slice }} +{{ with resources.GetRemote ( printf $url ) }} + {{ $lines := split .Content "\n" }} + {{ $totalLine := $lines | len }} + + {{ if ne $endLine -1 }} + {{ $endLine = math.Min $endLine $totalLine }} + {{ else }} + {{ $endLine = $totalLine }} + {{ end }} + + {{ if gt $startLine $endLine }} + {{ errorf "Code Importer Shortcode - startLine is greater than endLine" . }} + {{ end }} + + {{ $selectedLines := first $endLine $lines }} + {{ $selectedLines = after $startLine $selectedLines }} + {{ $codeBlock := printf "```%s\n%s\n```" $type (delimit $selectedLines "\n") }} + {{ $codeBlock | markdownify }} {{ else }} -{{ errorf "Code Importer Shortcode - Unable to get remote resource" . }} + {{ errorf "Code Importer Shortcode - Unable to get remote resource" . }} {{ end }}