Skip to content

Commit

Permalink
Merge pull request #3 from xoxys/add-anchor-copy
Browse files Browse the repository at this point in the history
Add anchor copy
  • Loading branch information
xoxys authored Feb 27, 2020
2 parents 0f4d728 + 3ecb62c commit e081e04
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
* FEATURE
* Add option to move anchor links to the left site of headlines
* add optional clipboard.js to copy anchor links on click
6 changes: 6 additions & 0 deletions exampleSite/content/usage/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@

# (Optional, default false) Move anchor link to the left side of headlines.
geekdocAnchorLeft = false

# (Optional, default true) Copy anchor url to clipboard on click.
geekdocAnchorCopy = true
```

{{< /tab >}}
Expand Down Expand Up @@ -113,6 +116,9 @@ params:

# (Optional, default false) Move anchor link to the left side of headlines.
geekdocAnchorLeft: false

# (Optional, default true) Copy anchor url to clipboard on click.
geekdocAnchorCopy: true
```
{{< /tab >}}
Expand Down
1 change: 1 addition & 0 deletions layouts/_default/baseof.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@
</div>
</body>

{{ partial "foot" . }}
</html>
4 changes: 2 additions & 2 deletions layouts/partials/content.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

{{ $.Scratch.Set "content" (.Content | replaceRE `<nav id="TableOfContents">\s*<ul>\s*<li>\s*<ul>` `<nav id="TableOfContents"><ul>` | replaceRE `</ul>\s*</li>\s*</ul>\s*</nav>` `</ul></nav>` | safeHTML) }}
{{ if and $showAnchor $anchorLeft }}
{{ $.Scratch.Set "content" ($.Scratch.Get "content" | replaceRE "(<h[2-9] id=\"([^\"]+)\"[^>]*>)(.*?)(</h[2-9]+>)" `${1}<a class="gdoc-page__anchor gdoc-page__anchor--left" href="#${2}"><svg class="icon link"><use xlink:href="#link"></use></svg></a>${3}${4}` | safeHTML) }}
{{ $.Scratch.Set "content" ($.Scratch.Get "content" | replaceRE "(<h[2-9] id=\"([^\"]+)\"[^>]*>)(.*?)(</h[2-9]+>)" (printf `%s<a data-clipboard-text="%s" class="gdoc-page__anchor gdoc-page__anchor--left clip" href="#%s"><svg class="icon link"><use xlink:href="#link"></use></svg></a>%s%s` `${1}` (absURL (printf "%s#%s" .Permalink `${2}`)) `${2}` `${3}` `${4}`) | safeHTML) }}
{{ else if and $showAnchor (not $anchorLeft) }}
{{ $.Scratch.Set "content" ($.Scratch.Get "content" | replaceRE "(<h[2-9] id=\"([^\"]+)\"[^>]*>)(.*?)(</h[2-9]+>)" `${1}${3}<a class="gdoc-page__anchor gdoc-page__anchor--right" href="#${2}"><svg class="icon link"><use xlink:href="#link"></use></svg></a>${4}` | safeHTML) }}
{{ $.Scratch.Set "content" ($.Scratch.Get "content" | replaceRE "(<h[2-9] id=\"([^\"]+)\"[^>]*>)(.*?)(</h[2-9]+>)" (printf `%s%s<a data-clipboard-text="%s" class="gdoc-page__anchor gdoc-page__anchor--right clip" href="#%s"><svg class="icon link"><use xlink:href="#link"></use></svg></a>%s` `${1}` `${3}` (absURL (printf "%s#%s" .Permalink `${2}`)) `${2}` `${4}`) | safeHTML) }}
{{ end }}

{{ $.Scratch.Get "content" }}
14 changes: 14 additions & 0 deletions layouts/partials/foot.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{{ if default true .Site.Params.GeekdocSearch }}
{{ .Scratch.Set "geekdocSearchConfig" .Site.Params.GeekdocSearchConfig }}
<!-- Remove after https://github.com/gohugoio/hugo/issues/6331 -->
{{ $searchJSFile := printf "js/%s.search.js" .Language.Lang }}
{{ $searchJS := resources.Get "js/search.js" | resources.ExecuteAsTemplate $searchJSFile . | resources.Minify }}
<script defer src="{{ $searchJS.RelPermalink }}"></script>
{{ end }}

{{ if default true .Site.Params.GeekdocAnchorCopy }}
<script src="{{ "js/clipboard.min.js" | relURL }}"></script>
<script>
var clipboard = new ClipboardJS('.clip');
</script>
{{ end }}
8 changes: 0 additions & 8 deletions layouts/partials/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,6 @@
<!-- Theme stylesheet, you can customize css by creating static/custom.css` in your website -->
<link rel="stylesheet" href="{{ "custom.css" | relURL }}">

{{ if default true .Site.Params.GeekdocSearch }}
{{ .Scratch.Set "geekdocSearchConfig" .Site.Params.GeekdocSearchConfig }}
<!-- Remove after https://github.com/gohugoio/hugo/issues/6331 -->
{{ $searchJSFile := printf "js/%s.search.js" .Language.Lang }}
{{ $searchJS := resources.Get "js/search.js" | resources.ExecuteAsTemplate $searchJSFile . | resources.Minify }}
<script defer src="{{ $searchJS.RelPermalink }}"></script>
{{ end }}

<!-- RSS -->
{{ with .OutputFormats.Get "rss" -}}
{{ printf `<link rel="%s" type="%s" href="%s" title="%s" />` .Rel .MediaType.Type .Permalink $.Site.Title | safeHTML }}
Expand Down
7 changes: 7 additions & 0 deletions static/js/clipboard.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e081e04

Please sign in to comment.