1
0
mirror of https://github.com/squidfunk/mkdocs-material.git synced 2024-11-30 18:24:35 +01:00

Fixed stats for private GitHub repositories

This commit is contained in:
squidfunk 2020-12-06 11:15:13 +01:00
parent b5353cbc67
commit ca05a2d342
11 changed files with 45 additions and 24 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1,8 +1,8 @@
{ {
"assets/javascripts/bundle.js": "assets/javascripts/bundle.7836ba4d.min.js", "assets/javascripts/bundle.js": "assets/javascripts/bundle.f81dfb4d.min.js",
"assets/javascripts/bundle.js.map": "assets/javascripts/bundle.7836ba4d.min.js.map", "assets/javascripts/bundle.js.map": "assets/javascripts/bundle.f81dfb4d.min.js.map",
"assets/javascripts/vendor.js": "assets/javascripts/vendor.fd16492e.min.js", "assets/javascripts/vendor.js": "assets/javascripts/vendor.0ac82a11.min.js",
"assets/javascripts/vendor.js.map": "assets/javascripts/vendor.fd16492e.min.js.map", "assets/javascripts/vendor.js.map": "assets/javascripts/vendor.0ac82a11.min.js.map",
"assets/javascripts/worker/search.js": "assets/javascripts/worker/search.4ac00218.min.js", "assets/javascripts/worker/search.js": "assets/javascripts/worker/search.4ac00218.min.js",
"assets/javascripts/worker/search.js.map": "assets/javascripts/worker/search.4ac00218.min.js.map", "assets/javascripts/worker/search.js.map": "assets/javascripts/worker/search.4ac00218.min.js.map",
"assets/stylesheets/main.css": "assets/stylesheets/main.19753c6b.min.css", "assets/stylesheets/main.css": "assets/stylesheets/main.19753c6b.min.css",

View File

@ -177,8 +177,8 @@
{% endblock %} {% endblock %}
</div> </div>
{% block scripts %} {% block scripts %}
<script src="{{ 'assets/javascripts/vendor.fd16492e.min.js' | url }}"></script> <script src="{{ 'assets/javascripts/vendor.0ac82a11.min.js' | url }}"></script>
<script src="{{ 'assets/javascripts/bundle.7836ba4d.min.js' | url }}"></script> <script src="{{ 'assets/javascripts/bundle.f81dfb4d.min.js' | url }}"></script>
{%- set translations = {} -%} {%- set translations = {} -%}
{%- for key in [ {%- for key in [
"clipboard.copy", "clipboard.copy",

View File

@ -22,7 +22,13 @@
import { Repo, User } from "github-types" import { Repo, User } from "github-types"
import { Observable, from } from "rxjs" import { Observable, from } from "rxjs"
import { map } from "rxjs/operators" import {
defaultIfEmpty,
filter,
map,
share,
switchMap
} from "rxjs/operators"
import { round } from "utilities" import { round } from "utilities"
@ -46,25 +52,29 @@ export function fetchSourceFactsFromGitHub(
const url = typeof repo !== "undefined" const url = typeof repo !== "undefined"
? `https://api.github.com/repos/${user}/${repo}` ? `https://api.github.com/repos/${user}/${repo}`
: `https://api.github.com/users/${user}` : `https://api.github.com/users/${user}`
return from(fetch(url).then(res => res.json())) return from(fetch(url))
.pipe( .pipe(
filter(res => res.status === 200),
switchMap(res => res.json()),
map(data => { map(data => {
/* GitHub repository */ /* GitHub repository */
if (typeof repo !== "undefined") { if (typeof repo !== "undefined") {
const { stargazers_count, forks_count }: Repo = data const { stargazers_count, forks_count }: Repo = data
return [ return [
`${round(stargazers_count || 0)} Stars`, `${round(stargazers_count!)} Stars`,
`${round(forks_count || 0)} Forks` `${round(forks_count!)} Forks`
] ]
/* GitHub user/organization */ /* GitHub user/organization */
} else { } else {
const { public_repos }: User = data const { public_repos }: User = data
return [ return [
`${round(public_repos || 0)} Repositories` `${round(public_repos!)} Repositories`
] ]
} }
}) }),
defaultIfEmpty([]),
share()
) )
} }

View File

@ -22,7 +22,13 @@
import { ProjectSchema } from "gitlab" import { ProjectSchema } from "gitlab"
import { Observable, from } from "rxjs" import { Observable, from } from "rxjs"
import { map } from "rxjs/operators" import {
defaultIfEmpty,
filter,
map,
share,
switchMap
} from "rxjs/operators"
import { round } from "utilities" import { round } from "utilities"
@ -44,11 +50,15 @@ export function fetchSourceFactsFromGitLab(
base: string, project: string base: string, project: string
): Observable<SourceFacts> { ): Observable<SourceFacts> {
const url = `https://${base}/api/v4/projects/${encodeURIComponent(project)}` const url = `https://${base}/api/v4/projects/${encodeURIComponent(project)}`
return from(fetch(url).then(res => res.json())) return from(fetch(url))
.pipe( .pipe(
filter(res => res.status === 200),
switchMap(res => res.json()),
map(({ star_count, forks_count }: ProjectSchema) => ([ map(({ star_count, forks_count }: ProjectSchema) => ([
`${round(star_count)} Stars`, `${round(star_count)} Stars`,
`${round(forks_count)} Forks` `${round(forks_count)} Forks`
])) ])),
defaultIfEmpty([]),
share()
) )
} }

View File

@ -21,7 +21,7 @@
*/ */
import { NEVER, Observable } from "rxjs" import { NEVER, Observable } from "rxjs"
import { catchError, map, switchMap } from "rxjs/operators" import { catchError, filter, map, switchMap } from "rxjs/operators"
import { getElementOrThrow, getElements } from "browser" import { getElementOrThrow, getElements } from "browser"
import { renderSource } from "templates" import { renderSource } from "templates"
@ -69,12 +69,12 @@ function fetchSourceFacts(
/* GitHub repository */ /* GitHub repository */
case "github": case "github":
const [, user, repo] = url.match(/^.+github\.com\/([^\/]+)\/?([^\/]+)?/i) const [, user, repo] = url.match(/^.+github\.com\/([^\/]+)\/?([^\/]+)?/i)!
return fetchSourceFactsFromGitHub(user, repo) return fetchSourceFactsFromGitHub(user, repo)
/* GitLab repository */ /* GitLab repository */
case "gitlab": case "gitlab":
const [, base, slug] = url.match(/^.+?([^\/]*gitlab[^\/]+)\/(.+?)\/?$/i) const [, base, slug] = url.match(/^.+?([^\/]*gitlab[^\/]+)\/(.+?)\/?$/i)!
return fetchSourceFactsFromGitLab(base, slug) return fetchSourceFactsFromGitLab(base, slug)
/* Everything else */ /* Everything else */
@ -104,6 +104,7 @@ export function patchSource(
switchMap(({ href }) => ( switchMap(({ href }) => (
cache(`${hash(href)}`, () => fetchSourceFacts(href)) cache(`${hash(href)}`, () => fetchSourceFacts(href))
)), )),
filter(facts => facts.length > 0),
catchError(() => NEVER) catchError(() => NEVER)
) )
.subscribe(facts => { .subscribe(facts => {