Extend hugo-theme-docdock to support multiple tutorials

Hi,
New to here. Thanks for help.
This question is about a theme, hugo-theme-docdock.
I want to extend it to support multiple tutorials. The reason is, suppose I have 100 tutorials, or books, it will be too big if I include all chapters of them in menu of each page.
What I want to do it something like, say, the file system is like,

books
-book1
---chap1 
---chap2 
-book2
---chap1 
---chap2

======
If I click chap1 in book1, the menu only has content about book1, something like,
-book1
—chap1
—chap2
If I am am reading book 2, the menu will be,
-book2
—chap1
—chap2

looks pretty easy to do, right?
but since I am new to hugo, and I meet some challenge when I try to use template and scratch, well, it cost we two days already.
Not only hugo grammar, but somtimes the output when I run hogo server, is random! each time the output is different.
It almost drive me crazy.
so, I come here for help.
below is the code,
thanks!

======

{{$currentNode := .}}

{{$nodes := split $currentNode.URL  "/"}}
{{$.Scratch.Set "isBook" false}}
{{$.Scratch.Set "BookName" "NoName"}}
{{$.Scratch.Set "index" 0}}

{{range $nodes}}
	{{$.Scratch.Set "val" .}}
	{{$index := int ($.Scratch.Get "index")}}
	{{if eq $index 1}}
		{{if eq ($.Scratch.Get "val") "books"}}
			{{$.Scratch.Set "isBook" true}}
		{{end}}
	{{end}}
	{{if eq $index 2}}
		{{$.Scratch.Set "BookName" .}}
	{{end}}		
	{{$.Scratch.Set "index" (add $index  1)}}
{{end}}

{{if eq ($.Scratch.Get "BookName") ""}}
	{{$.Scratch.Set "isBook" false}}
{{end}}
{{if not ($.Scratch.Get "isBook") }}
	{{$.Scratch.Set "BookName" ""}}
{{end}}

{{print (.Scratch.Get "isBook")}}
{{print (.Scratch.Get "BookName")}}

{{$ifShow := not ((.Scratch.Get "isBook"))}}

{{$showvisitedlinks := .Site.Params.showVisitedLinks}}

{{if eq .Site.Params.ordersectionsby "title"}}
	{{range .Site.Home.Sections.ByTitle}}
		{{template "section-tree-nav" dict "sect" . "currentnode" $currentNode "showvisitedlinks" $showvisitedlinks "ifShow" $ifShow}}
	{{end}}
{{else}}
	{{range .Site.Home.Sections.ByWeight}}
		{{template "section-tree-nav" dict "sect" . "currentnode" $currentNode "showvisitedlinks" $showvisitedlinks "ifShow" $ifShow}}
	{{end}}
{{end}}


	
<!-- templates -->
{{define "section-tree-nav"}}
	{{$showvisitedlinks := .showvisitedlinks}}
	{{$currentNode := .currentnode}}
	{{$ifShow := .ifShow}}

	{{with .sect}}
		{{.Scratch.Set "ifShow" $ifShow}}
		{{if eq .URL  "/books/"}}
			{{.Scratch.Set "ifShow" (not $ifShow)}}	
		{{end}}
		{{$ifShow := (.Scratch.Get "ifShow")}}
		{{if and .IsSection (or (not .Params.hidden) $.showhidden)}}
			{{$numberOfPages := (add (len .Pages) (len .Sections))}}
			{{if $ifShow }}
				{{safeHTML .Params.head}}
				<li data-nav-id="{{.URL}}" class="dd-item
					{{if .IsAncestor $currentNode}} parent{{end}}
					{{if eq .URL $currentNode.URL}} active{{end}}
					{{if .Params.alwaysopen}} alwaysopen{{end}}
				">
				<a href="{{.RelPermalink}}">
					<span>{{safeHTML .Params.Pre}}{{.Title}}{{safeHTML .Params.Post}}</span>
					{{if ne $numberOfPages 0}}
						{{if or (.IsAncestor $currentNode) (.Params.alwaysopen)}}
							<i class="fa fa-angle-down fa-lg category-icon"></i>
						{{else}}
							<i class="fa fa-angle-right fa-lg category-icon"></i>
						{{end}}
					{{end}}

					{{if $showvisitedlinks}}<i class="fa fa-circle-thin read-icon"></i>{{end}}
				</a>
			{{end}}
			{{if ne $numberOfPages 0}}
				{{if $ifShow }}<ul>{{end}}
				{{.Scratch.Set "pages" .Pages}}
				{{if .Sections}}
					{{.Scratch.Set "pages" (.Pages | union .Sections)}}
				{{end}}
				{{$pages := (.Scratch.Get "pages")}}
				
				{{if eq .Site.Params.ordersectionsby "title"}}
					{{range $pages.ByTitle}}
						{{if and .Params.hidden (not $.showhidden)}}
						{{else}}
							{{template "section-tree-nav" dict "sect" . "currentnode" $currentNode "showvisitedlinks" $showvisitedlinks "ifShow" $ifShow}}
						{{end}}
					{{end}}
				{{else}}
					{{range $pages.ByWeight}}
						{{if and .Params.hidden (not $.showhidden)}}
						{{else}}
							{{template "section-tree-nav" dict "sect" . "currentnode" $currentNode "showvisitedlinks" $showvisitedlinks "ifShow" $ifShow}}
						{{end}}
					{{end}}
				{{end}}
				{{if $ifShow }}</ul>{{end}}
			{{end}}
			{{if $ifShow }}</li>{{end}}		
		{{else}}
			{{if and ( not .Params.Hidden) ($ifShow)}}
				<li data-nav-id="{{.URL}}" class="dd-item
				{{if eq .URL $currentNode.URL}} active{{end}}
				">
				<a href="{{.RelPermalink}}">
				<span>{{safeHTML .Params.Pre}}{{.Title}}{{safeHTML .Params.Post}}</span>
				{{if $showvisitedlinks}}<i class="fa fa-circle-thin read-icon"></i>{{end}}
				</a>
				</li>
			{{end}}
		{{end}}
	{{end}}
{{end}}

=======