-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
136 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
{{- /* | ||
* This template is used to create a post for each repo in the data file. | ||
* https://github.com/hugo-fixit/component-projects | ||
* | ||
* How to use: | ||
* Create _content.gotmpl in projects section folder. | ||
* Add the following code to the _content.gotmpl file: | ||
* {{- partial "adapters/projects.html" . -}} | ||
* | ||
* Directory structure: | ||
* content/ | ||
* ├── projects/ | ||
* │ ├── _content.gotmpl <-- content adapter | ||
* │ └── _index.md <-- layout: projects | ||
* data/ | ||
* └── projects.yml <-- projects data | ||
* | ||
* @author Lruihao (https://lruihao.cn) | ||
*/ -}} | ||
|
||
{{- $auth := dict "Authorization" "" -}} | ||
{{- with (getenv "HUGO_PARAMS_GHTOKEN") -}} | ||
{{- $auth = dict "Authorization" (printf "token %v" .) -}} | ||
{{- end -}} | ||
{{- $headers := dict "headers" $auth -}} | ||
{{- $projectsAdapters := .Site.Params.projectsAdapters -}} | ||
{{- $data := index .Site.Data (printf "projects.%v" .Site.Language.Lang) | default .Site.Data.projects -}} | ||
{{- $renderedRepos := slice -}} | ||
|
||
{{- if $projectsAdapters.enable -}} | ||
{{- warnf "component-projects\n%v" (T "projects.warningAdapter") -}} | ||
{{- /* Loop through the data and create a post for each repo */ -}} | ||
{{- range $group := $data -}} | ||
{{- if eq $group.contentAdapters false -}} | ||
{{- continue -}} | ||
{{- end -}} | ||
{{- range $repo := $group.repos -}} | ||
{{- /* Skip if the repo is in the ignore list or has been rendered */ -}} | ||
{{- if (in $projectsAdapters.ignoreList $repo) | or (in $renderedRepos $repo) -}} | ||
{{- continue -}} | ||
{{- end -}} | ||
{{- $projectsAdapters.ignoreList -}} | ||
{{- $repoAPI := printf "https://api.github.com/repos/%v" $repo -}} | ||
{{- $repoInfo := partial "function/get-remote-json" (dict "URL" $repoAPI "OPTIONS" $headers) -}} | ||
{{- $readmeAPI := printf "https://api.github.com/repos/%v/contents/README.%v.md" $repo $.Site.Language.Lang -}} | ||
{{- $readme := partial "function/get-remote-json" (dict "URL" $readmeAPI "OPTIONS" $headers) -}} | ||
|
||
{{- /* Get the default README if the multi-language README is not available */ -}} | ||
{{- if not $readme -}} | ||
{{- $readmeAPI = printf "https://api.github.com/repos/%v/readme" $repo -}} | ||
{{- $readme = partial "function/get-remote-json" (dict "URL" $readmeAPI "OPTIONS" $headers) -}} | ||
{{- end -}} | ||
|
||
{{- /* Skip if the repo info or README is not available */ -}} | ||
{{- if | ||
(not $repoInfo) | or | ||
(not $readme) | or | ||
(and $projectsAdapters.onlyPublic (eq $repoInfo.visibility "private")) | ||
-}} | ||
{{- continue -}} | ||
{{- end -}} | ||
|
||
{{- /* Add the repo to the rendered list */ -}} | ||
{{- $renderedRepos = $renderedRepos | append $repo -}} | ||
|
||
{{- /* Post dates from repo info */ -}} | ||
{{- $dates := dict | ||
"date" (time.AsTime $repoInfo.created_at) | ||
"lastmod" (time.AsTime $repoInfo.updated_at) | ||
-}} | ||
{{- /* Post params from repo info (equivalent to the front matter of posts) */ -}} | ||
{{- $author := dict | ||
"name" $repoInfo.owner.login | ||
"link" $repoInfo.owner.html_url | ||
"avatar" $repoInfo.owner.avatar_url | ||
-}} | ||
{{- $categories := $group.categories | default $projectsAdapters.categories -}} | ||
{{- $collections := $group.collections | default $projectsAdapters.collections -}} | ||
{{- $subtitle := T "projects.source" (dict | ||
"Name" $repoInfo.full_name | ||
"Link" $repoInfo.html_url | ||
"Website" $repoInfo.homepage | ||
) -}} | ||
{{- $params := dict | ||
"fromAdapters" "projects" | ||
"author" $author | ||
"categories" $categories | ||
"collections" $collections | ||
"tags" $repoInfo.topics | ||
"lightgallery" true | ||
"capitalizeTitles" false | ||
"toc" (dict "ordered" false) | ||
"subtitle" $subtitle | ||
"sourceURL" (add $readme.html_url "?plain=1") | ||
"editURL" (replace $readme.html_url "/blob/" "/edit/" ) | ||
"LinkToReport" false | ||
-}} | ||
{{- /* Post content from repo README */ -}} | ||
{{- $markdown := $readme.content | base64Decode -}} | ||
{{- /* Convert Hugo shortcodes to markdown plain text */ -}} | ||
{{- $markdown = replace $markdown "{{<" "{{?{}<" -}} | ||
{{- /* Convert relative links to absolute links in the README content */ -}} | ||
{{- $replacement := printf "$1[$2](%v$4)" (printf "https://raw.githubusercontent.com/%v/refs/heads/%v/$3" $repoInfo.full_name $repoInfo.default_branch) -}} | ||
{{- /* Oops! Lookahead regular lookup is not supported in Golang, e.g `(?!http)` */ -}} | ||
{{- $markdown = replaceRE `([!]?)\[(.*?)\]\(([/|\./].*).*?(\s+".*?")?\)` $replacement $markdown -}} | ||
{{- $content := dict | ||
"mediaType" "text/markdown" | ||
"value" $markdown | ||
-}} | ||
{{- /* Create a post page */ -}} | ||
{{- $page := dict | ||
"title" $repoInfo.description | ||
"linkTitle" $repoInfo.full_name | ||
"path" $repoInfo.full_name | ||
"dates" $dates | ||
"description" (printf "%s: %s" (replace $repoInfo.full_name "-" " ") $repoInfo.description) | ||
"content" $content | ||
"params" $params | ||
"type" "posts" | ||
-}} | ||
{{- $.AddPage $page -}} | ||
{{- end -}} | ||
{{- end -}} | ||
{{- end -}} |