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 1/4] 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 }} From 6b4480ae83f09cd2f96d475a6977218096db7a5a Mon Sep 17 00:00:00 2001 From: jason810496 <810496@email.wlsh.tyc.edu.tw> Date: Fri, 21 Jun 2024 23:59:10 +0800 Subject: [PATCH 2/4] docs: add codeimpoter startLine endLine docs --- exampleSite/content/docs/shortcodes/index.it.md | 9 +++++++++ exampleSite/content/docs/shortcodes/index.ja.md | 10 +++++++++- exampleSite/content/docs/shortcodes/index.md | 9 +++++++++ exampleSite/content/docs/shortcodes/index.zh-cn.md | 9 +++++++++ 4 files changed, 36 insertions(+), 1 deletion(-) diff --git a/exampleSite/content/docs/shortcodes/index.it.md b/exampleSite/content/docs/shortcodes/index.it.md index ba1bd718..1370a69a 100644 --- a/exampleSite/content/docs/shortcodes/index.it.md +++ b/exampleSite/content/docs/shortcodes/index.it.md @@ -197,6 +197,8 @@ This shortcode is for importing code from external sources easily without copyin | --------- | ------------------------------------------------------- | | `url` | **Required** URL to an externally hosted code file. | | `type` | Code type used for syntax highlighting. | +| `startLine` | Optional. The line number to start the import from. | +| `endLine` | Optional. The line number to end the import at. | @@ -210,6 +212,13 @@ This shortcode is for importing code from external sources easily without copyin ``` {{< codeimporter url="https://raw.githubusercontent.com/nunocoracao/blowfish/main/layouts/shortcodes/mdimporter.html" type="go" >}} +```md +{{}} + +``` + +{{< codeimporter url="https://raw.githubusercontent.com/nunocoracao/blowfish/main/config/_default/hugo.toml" type="c" startLine="11" endLine="18">}} +

diff --git a/exampleSite/content/docs/shortcodes/index.ja.md b/exampleSite/content/docs/shortcodes/index.ja.md index ba1bd718..ac93cb85 100644 --- a/exampleSite/content/docs/shortcodes/index.ja.md +++ b/exampleSite/content/docs/shortcodes/index.ja.md @@ -197,7 +197,8 @@ This shortcode is for importing code from external sources easily without copyin | --------- | ------------------------------------------------------- | | `url` | **Required** URL to an externally hosted code file. | | `type` | Code type used for syntax highlighting. | - +| `startLine` | Optional. The line number to start the import from. | +| `endLine` | Optional. The line number to end the import at. | @@ -210,6 +211,13 @@ This shortcode is for importing code from external sources easily without copyin ``` {{< codeimporter url="https://raw.githubusercontent.com/nunocoracao/blowfish/main/layouts/shortcodes/mdimporter.html" type="go" >}} +```md +{{}} + +``` + +{{< codeimporter url="https://raw.githubusercontent.com/nunocoracao/blowfish/main/config/_default/hugo.toml" type="c" startLine="11" endLine="18">}} +

diff --git a/exampleSite/content/docs/shortcodes/index.md b/exampleSite/content/docs/shortcodes/index.md index 9bb3f042..d8a3cb7b 100644 --- a/exampleSite/content/docs/shortcodes/index.md +++ b/exampleSite/content/docs/shortcodes/index.md @@ -197,6 +197,8 @@ This shortcode is for importing code from external sources easily without copyin | --------- | ------------------------------------------------------- | | `url` | **Required** URL to an externally hosted code file. | | `type` | Code type used for syntax highlighting. | +| `startLine` | Optional. The line number to start the import from. | +| `endLine` | Optional. The line number to end the import at. | @@ -210,6 +212,13 @@ This shortcode is for importing code from external sources easily without copyin ``` {{< codeimporter url="https://raw.githubusercontent.com/nunocoracao/blowfish/main/layouts/shortcodes/mdimporter.html" type="go" >}} +```md +{{}} + +``` + +{{< codeimporter url="https://raw.githubusercontent.com/nunocoracao/blowfish/main/config/_default/hugo.toml" type="c" startLine="11" endLine="18">}} +

diff --git a/exampleSite/content/docs/shortcodes/index.zh-cn.md b/exampleSite/content/docs/shortcodes/index.zh-cn.md index 91c0f483..2696eeff 100644 --- a/exampleSite/content/docs/shortcodes/index.zh-cn.md +++ b/exampleSite/content/docs/shortcodes/index.zh-cn.md @@ -197,6 +197,8 @@ data: { | --------- | ---------------------------------- | | `url` | **必需的** 外部托管代码文件的 URL. | | `type` | 用于语法突出显示的代码类型. | +| `startLine` | **可选** 从代码文件中导入的起始行. | +| `endLine` | **可选** 从代码文件中导入的结束行. | @@ -211,6 +213,13 @@ data: { {{< codeimporter url="https://raw.githubusercontent.com/nunocoracao/blowfish/main/layouts/shortcodes/mdimporter.html" type="go" >}} +```md +{{}} + +``` + +{{< codeimporter url="https://raw.githubusercontent.com/nunocoracao/blowfish/main/config/_default/hugo.toml" type="c" startLine="11" endLine="18">}} +

From 8a710a5f1a2b0835549d765b57314e6b3149cb60 Mon Sep 17 00:00:00 2001 From: jason810496 <810496@email.wlsh.tyc.edu.tw> Date: Sat, 22 Jun 2024 00:10:40 +0800 Subject: [PATCH 3/4] docs: fix codeimporter docs example --- exampleSite/content/docs/shortcodes/index.it.md | 8 ++++---- exampleSite/content/docs/shortcodes/index.ja.md | 8 ++++---- exampleSite/content/docs/shortcodes/index.md | 8 ++++---- exampleSite/content/docs/shortcodes/index.zh-cn.md | 4 ++-- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/exampleSite/content/docs/shortcodes/index.it.md b/exampleSite/content/docs/shortcodes/index.it.md index 1370a69a..4102673b 100644 --- a/exampleSite/content/docs/shortcodes/index.it.md +++ b/exampleSite/content/docs/shortcodes/index.it.md @@ -197,8 +197,8 @@ This shortcode is for importing code from external sources easily without copyin | --------- | ------------------------------------------------------- | | `url` | **Required** URL to an externally hosted code file. | | `type` | Code type used for syntax highlighting. | -| `startLine` | Optional. The line number to start the import from. | -| `endLine` | Optional. The line number to end the import at. | +| `startLine` | **Optional** The line number to start the import from. | +| `endLine` | **Optional** The line number to end the import at. | @@ -213,11 +213,11 @@ This shortcode is for importing code from external sources easily without copyin {{< codeimporter url="https://raw.githubusercontent.com/nunocoracao/blowfish/main/layouts/shortcodes/mdimporter.html" type="go" >}} ```md -{{}} +{{}} ``` -{{< codeimporter url="https://raw.githubusercontent.com/nunocoracao/blowfish/main/config/_default/hugo.toml" type="c" startLine="11" endLine="18">}} +{{< codeimporter url="https://raw.githubusercontent.com/nunocoracao/blowfish/main/config/_default/hugo.toml" type="toml" startLine="11" endLine="18">}}

diff --git a/exampleSite/content/docs/shortcodes/index.ja.md b/exampleSite/content/docs/shortcodes/index.ja.md index ac93cb85..a93cc237 100644 --- a/exampleSite/content/docs/shortcodes/index.ja.md +++ b/exampleSite/content/docs/shortcodes/index.ja.md @@ -197,8 +197,8 @@ This shortcode is for importing code from external sources easily without copyin | --------- | ------------------------------------------------------- | | `url` | **Required** URL to an externally hosted code file. | | `type` | Code type used for syntax highlighting. | -| `startLine` | Optional. The line number to start the import from. | -| `endLine` | Optional. The line number to end the import at. | +| `startLine` | **Optional** The line number to start the import from. | +| `endLine` | **Optional** The line number to end the import at. | @@ -212,11 +212,11 @@ This shortcode is for importing code from external sources easily without copyin {{< codeimporter url="https://raw.githubusercontent.com/nunocoracao/blowfish/main/layouts/shortcodes/mdimporter.html" type="go" >}} ```md -{{}} +{{}} ``` -{{< codeimporter url="https://raw.githubusercontent.com/nunocoracao/blowfish/main/config/_default/hugo.toml" type="c" startLine="11" endLine="18">}} +{{< codeimporter url="https://raw.githubusercontent.com/nunocoracao/blowfish/main/config/_default/hugo.toml" type="toml" startLine="11" endLine="18">}}

diff --git a/exampleSite/content/docs/shortcodes/index.md b/exampleSite/content/docs/shortcodes/index.md index d8a3cb7b..29cb834b 100644 --- a/exampleSite/content/docs/shortcodes/index.md +++ b/exampleSite/content/docs/shortcodes/index.md @@ -197,8 +197,8 @@ This shortcode is for importing code from external sources easily without copyin | --------- | ------------------------------------------------------- | | `url` | **Required** URL to an externally hosted code file. | | `type` | Code type used for syntax highlighting. | -| `startLine` | Optional. The line number to start the import from. | -| `endLine` | Optional. The line number to end the import at. | +| `startLine` | **Optional** The line number to start the import from. | +| `endLine` | **Optional** The line number to end the import at. | @@ -213,11 +213,11 @@ This shortcode is for importing code from external sources easily without copyin {{< codeimporter url="https://raw.githubusercontent.com/nunocoracao/blowfish/main/layouts/shortcodes/mdimporter.html" type="go" >}} ```md -{{}} +{{}} ``` -{{< codeimporter url="https://raw.githubusercontent.com/nunocoracao/blowfish/main/config/_default/hugo.toml" type="c" startLine="11" endLine="18">}} +{{< codeimporter url="https://raw.githubusercontent.com/nunocoracao/blowfish/main/config/_default/hugo.toml" type="toml" startLine="11" endLine="18">}}

diff --git a/exampleSite/content/docs/shortcodes/index.zh-cn.md b/exampleSite/content/docs/shortcodes/index.zh-cn.md index 2696eeff..25bc4cdb 100644 --- a/exampleSite/content/docs/shortcodes/index.zh-cn.md +++ b/exampleSite/content/docs/shortcodes/index.zh-cn.md @@ -214,11 +214,11 @@ data: { {{< codeimporter url="https://raw.githubusercontent.com/nunocoracao/blowfish/main/layouts/shortcodes/mdimporter.html" type="go" >}} ```md -{{}} +{{}} ``` -{{< codeimporter url="https://raw.githubusercontent.com/nunocoracao/blowfish/main/config/_default/hugo.toml" type="c" startLine="11" endLine="18">}} +{{< codeimporter url="https://raw.githubusercontent.com/nunocoracao/blowfish/main/config/_default/hugo.toml" type="toml" startLine="11" endLine="18">}}

From c91bc9dfb59e39fc0d719ffe9ea6b9beaf58a991 Mon Sep 17 00:00:00 2001 From: jason810496 <810496@email.wlsh.tyc.edu.tw> Date: Sat, 22 Jun 2024 23:37:23 +0800 Subject: [PATCH 4/4] fix: handle codeimporter URL encoding --- layouts/shortcodes/codeimporter.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/layouts/shortcodes/codeimporter.html b/layouts/shortcodes/codeimporter.html index d461b5bb..952c28a8 100644 --- a/layouts/shortcodes/codeimporter.html +++ b/layouts/shortcodes/codeimporter.html @@ -4,7 +4,7 @@ {{ $startLine = sub $startLine 1 }} {{ $endLine := .Get "endLine" | default -1 | int }} {{ $selectedLines := slice }} -{{ with resources.GetRemote ( printf $url ) }} +{{ with resources.GetRemote (urls.Parse $url) }} {{ $lines := split .Content "\n" }} {{ $totalLine := $lines | len }}