Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add startLine, endLine Support for codeimporter #1558

Merged
merged 4 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions exampleSite/content/docs/shortcodes/index.it.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |


<!-- prettier-ignore-end -->
Expand All @@ -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="toml" startLine="11" endLine="18" */>}}

```

{{< codeimporter url="https://raw.githubusercontent.com/nunocoracao/blowfish/main/config/_default/hugo.toml" type="toml" startLine="11" endLine="18">}}


<br/><br/>

Expand Down
10 changes: 9 additions & 1 deletion exampleSite/content/docs/shortcodes/index.ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |

<!-- prettier-ignore-end -->

Expand All @@ -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="toml" startLine="11" endLine="18" */>}}

```

{{< codeimporter url="https://raw.githubusercontent.com/nunocoracao/blowfish/main/config/_default/hugo.toml" type="toml" startLine="11" endLine="18">}}


<br/><br/>

Expand Down
9 changes: 9 additions & 0 deletions exampleSite/content/docs/shortcodes/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |


<!-- prettier-ignore-end -->
Expand All @@ -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="toml" startLine="11" endLine="18" */>}}

```

{{< codeimporter url="https://raw.githubusercontent.com/nunocoracao/blowfish/main/config/_default/hugo.toml" type="toml" startLine="11" endLine="18">}}


<br/><br/>

Expand Down
9 changes: 9 additions & 0 deletions exampleSite/content/docs/shortcodes/index.zh-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ data: {
| --------- | ---------------------------------- |
| `url` | **必需的** 外部托管代码文件的 URL. |
| `type` | 用于语法突出显示的代码类型. |
| `startLine` | **可选** 从代码文件中导入的起始行. |
| `endLine` | **可选** 从代码文件中导入的结束行. |


<!-- prettier-ignore-end -->
Expand All @@ -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="toml" startLine="11" endLine="18" */>}}

```

{{< codeimporter url="https://raw.githubusercontent.com/nunocoracao/blowfish/main/config/_default/hugo.toml" type="toml" startLine="11" endLine="18">}}


<br/><br/>

Expand Down
25 changes: 22 additions & 3 deletions layouts/shortcodes/codeimporter.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
{{ $url := .Get "url" }}
{{ $type := .Get "type" }}
{{ $startLine := .Get "startLine" | default 1 | int }}
{{ $startLine = sub $startLine 1 }}
{{ $endLine := .Get "endLine" | default -1 | int }}
{{ $selectedLines := slice }}
{{ with resources.GetRemote (urls.Parse $url) }}
{{ $codeBlock := printf "```%s\n%s\n```" $type .Content }}
{{ $codeBlock | markdownify }}
{{ $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 }}