mirror of
https://github.com/hugo-fixit/FixIt.git
synced 2026-08-24 15:28:57 +00:00
feat(File Tree): add file-tree shortcode support (#685)
* feat(File Tree): add `file-tree` shortcode support * feat(file-tree): add ignoreList parameter to file-tree shortcode * feat(file-tree): enhance file-tree shortcode with ignoreList and folderSlash options * feat(file-tree): enhance file-tree shortcode to support data mode * feat(file-tree): enhance file-tree shortcode to support file mode * feat(file-tree): improve content parsing in file-tree shortcode * feat(file-tree): improve root path validation and error handling in filesystem mode * test(file-tree): add unit tests for `file-tree` shortcode * feat(file-tree): add file-tree code block rendering support * refactor(file-tree): extract filesystem scanning to get-file-tree partial Move directory traversal and root path validation to a separate partial function, consolidating all filesystem-related logic in one place. This simplifies the shortcode implementation and ensures a unified data format for rendering. * docs(readme): extend code fence and shortcode support for file tree in documentation * feat(file-tree): add fullRootName parameter for root path display in file tree * feat(file-tree): add language parameter to get-file-tree function * feat(file-tree): add highlightList parameter for file-tree shortcode * perf(file-tree): add level param for get-file-tree function to descend only level directories deep
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
{{- /*
|
||||
Recursive function to render tree from structured data
|
||||
@param {array} items - Array of tree items
|
||||
@param {int} depth - Current depth level
|
||||
@param {int} level - The expand level of the tree (expand all: -1, collapse all: 0)
|
||||
@param {bool} folderSlash - Whether to append a trailing "/" to folder names
|
||||
@param {array} ignoreList - List of file or folder names to ignore
|
||||
@param {array} highlightList - List of file or folder names to highlight
|
||||
|
||||
File tree item structure:
|
||||
name: string - The name of the file or folder
|
||||
type: string - Type of item, either "dir" (directory) or "file"
|
||||
children?: array - (Optional) Array of child items, only valid for directories
|
||||
*/ -}}
|
||||
{{- define "render-file-tree" -}}
|
||||
{{- $items := .items -}}
|
||||
{{- $depth := .depth -}}
|
||||
{{- $level := .level -}}
|
||||
{{- $folderSlash := .folderSlash -}}
|
||||
{{- $ignoreList := .ignoreList | default slice -}}
|
||||
{{- $highlightList := .highlightList -}}
|
||||
|
||||
<ul class="file-tree" data-level="{{ $depth }}">
|
||||
{{- range $items -}}
|
||||
{{- if in $ignoreList .name -}}
|
||||
{{- continue -}}
|
||||
{{- end -}}
|
||||
{{- $isDir := eq .type "dir" -}}
|
||||
{{- $isCollapsed := $isDir | and (ge $depth $level) | and (ge $level 0) -}}
|
||||
{{- $isHighlighted := in $highlightList .name -}}
|
||||
{{- $label := .name -}}
|
||||
{{- if and $isDir $folderSlash -}}
|
||||
{{- $label = printf "%s/" $label -}}
|
||||
{{- end -}}
|
||||
|
||||
<li class="file-tree-item{{ if $isDir }} file-tree-folder{{ end }}{{ if $isCollapsed }} is-collapsed{{ end }}">
|
||||
<span class="file-tree-label{{ if $isDir }} file-tree-toggle{{ end }}{{ if $isHighlighted }} is-highlighted{{ end }}">
|
||||
{{- $icon := "file-tree-icon fa-regular " -}}
|
||||
{{- if $isDir -}}
|
||||
{{- $icon = add $icon "fa-folder-open" -}}
|
||||
{{- else -}}
|
||||
{{- $icon = add $icon "fa-file-lines" -}}
|
||||
{{- end -}}
|
||||
{{- dict "Class" $icon | partial "plugin/icon.html" -}}
|
||||
<span class="file-tree-label__inner">{{ $label }}</span>
|
||||
</span>
|
||||
|
||||
{{- if and $isDir .children -}}
|
||||
{{- $options := dict
|
||||
"items" .children
|
||||
"depth" (add $depth 1)
|
||||
"level" $level
|
||||
"folderSlash" $folderSlash
|
||||
"ignoreList" $ignoreList
|
||||
"highlightList" $highlightList
|
||||
-}}
|
||||
{{- template "render-file-tree" $options -}}
|
||||
{{- end -}}
|
||||
</li>
|
||||
{{- end -}}
|
||||
</ul>
|
||||
{{- end -}}
|
||||
|
||||
{{- if .items -}}
|
||||
<div class="file-tree-wrapper">
|
||||
{{- $options := dict "depth" 0 | merge . -}}
|
||||
{{- template "render-file-tree" $options -}}
|
||||
</div>
|
||||
{{- end -}}
|
||||
Reference in New Issue
Block a user