Skip to content

Commit

Permalink
Feat: 增加内容适配器复用模板
Browse files Browse the repository at this point in the history
  • Loading branch information
Lruihao committed Oct 6, 2024
1 parent 5d43f37 commit 0b4ee31
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 114 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,17 @@ Use the `gh-repo-card-container` and `gh-repo-card` shortcodes in any page to di

:tada: This is a awesome feature! It can generate blog posts from the README of the repositories according to the projects data you configured.

You can copy the [content adapter template](/_content.gotmpl) of this component to your project:
Create `_content.gotmpl` in projects section folder, add the following code:

```go-html-template
{{- partial "adapters/projects.html" . -}}
```

```plain
content/
├── projects/
│ ├── _content.gotmpl <-- content adapter
│ └── _index.md
│ └── _index.md <-- layout: projects
data/
└── projects.yml <-- projects data
```
Expand Down
8 changes: 6 additions & 2 deletions README.zh-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,17 @@ Some text to display at the start of the page.

:tada: 这是一个很棒的功能!它能够根据你配置的项目数据,把仓库的 README 自动生成博客文章。

你可以复制本组件的 [内容适配器模板](/_content.gotmpl) 到你的项目中:
在项目文件夹中创建 `_content.gotmpl`,添加以下代码:

```go-html-template
{{- partial "adapters/projects.html" . -}}
```

```plain
content/
├── projects/
│ ├── _content.gotmpl <-- content adapter
│ └── _index.md
│ └── _index.md <-- layout: projects
data/
└── projects.yml <-- projects data
```
Expand Down
110 changes: 0 additions & 110 deletions _content.gotmpl

This file was deleted.

124 changes: 124 additions & 0 deletions layouts/partials/adapters/projects.html
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 -}}

0 comments on commit 0b4ee31

Please sign in to comment.