Skip to content

Commit f78fb92

Browse files
committed
feat(parsecontents): rename parseReleases into parseContents, add remark-github on readme & realease
1 parent 11fd574 commit f78fb92

File tree

10 files changed

+395
-394
lines changed

10 files changed

+395
-394
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"@nuxt/kit": "^3.0.0-rc.5",
3030
"@octokit/graphql": "^5.0.0",
3131
"@octokit/rest": "^19.0.3",
32+
"remark-gfm": "^3.0.1",
3233
"remark-github": "^11.2.4"
3334
},
3435
"devDependencies": {

src/module.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ export interface ModuleOptions extends GithubRepositoryOptions {
1313
contributors?: boolean
1414
maxContributors?: number
1515
/**
16-
* Parse release notes markdown and return AST tree
16+
* Parse contents (releases content, readme) Markdown and return AST tree.
1717
*
1818
* Note: This option is only available when you have `@nuxt/content` installed in your project.
1919
*
2020
* @default true
2121
*/
22-
parseReleases?: boolean
22+
parseContents?: boolean
2323
}
2424

2525
declare module '@nuxt/schema' {
@@ -49,7 +49,7 @@ export default defineNuxtModule<ModuleOptions>({
4949
releases: true,
5050
contributors: true,
5151
maxContributors: 100,
52-
parseReleases: true
52+
parseContents: true
5353
},
5454
setup (options, nuxt) {
5555
const { resolve } = createResolver(import.meta.url)
@@ -73,7 +73,7 @@ export default defineNuxtModule<ModuleOptions>({
7373
branch: options.branch || process.env.GITHUB_BRANCH,
7474
repo: options.repo || process.env.GITHUB_REPO,
7575
token: options.token || process.env.GITHUB_TOKEN,
76-
parseReleases: options.parseReleases,
76+
parseContents: options.parseContents,
7777
maxContributors: options.maxContributors
7878
}
7979

src/runtime/server/api/contributors/file.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { useQuery } from 'h3'
22
import type { ModuleOptions } from '../../../../module'
33
import { fetchFileContributors, overrideConfig } from '../../utils/queries'
44
import { GithubContributorsQuery } from '../../../types'
5+
// @ts-ignore
56
import * as imports from '#imports'
67

78
let handler

src/runtime/server/api/contributors/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { useQuery } from 'h3'
22
import type { ModuleOptions } from '../../../../module'
33
import { fetchRepositoryContributors, overrideConfig } from '../../utils/queries'
44
import { GithubContributorsQuery } from '../../../types'
5+
// @ts-ignore
56
import * as imports from '#imports'
67

78
let handler

src/runtime/server/api/readme.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,25 @@ export default handler(
3333
// Fetch readme from GitHub
3434
const readme = await fetchReadme(githubConfig) as GithubRepositoryReadme
3535

36-
// Return parsed content
37-
return await parseContent(`${githubConfig.owner}:${githubConfig.repo}:readme.md`, Buffer.from(readme.content, 'base64').toString())
36+
// Readme readable content
37+
const content = Buffer.from(readme.content, 'base64').toString()
38+
39+
// Parse contents with @nuxt/content if enabled
40+
if (moduleConfig.parseContents) {
41+
// Return parsed content
42+
return await parseContent(`${githubConfig.owner}:${githubConfig.repo}:readme.md`, content, {
43+
markdown: {
44+
remarkPlugins: {
45+
// Use current Github repository for remark-github plugin
46+
'remark-github': {
47+
repository: `${githubConfig.owner}/${githubConfig.repo}`
48+
}
49+
}
50+
}
51+
})
52+
}
53+
54+
return content
3855
},
3956
{
4057
maxAge: 60 // cache for one minute

src/runtime/server/api/releases/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { useQuery } from 'h3'
22
import { fetchReleases, overrideConfig, parseRelease } from '../../utils/queries'
33
import type { ModuleOptions } from '../../../../module'
44
import { GithubRawRelease, GithubReleasesQuery } from '../../../types'
5+
// @ts-ignore
56
import * as imports from '#imports'
67

78
let handler
@@ -33,8 +34,8 @@ export default handler(
3334
if (!releases) { return }
3435

3536
// Parse release notes when `parse` option is enabled and `@nuxt/content` is installed.
36-
if (moduleConfig.parseReleases) {
37-
releases = await Promise.all(releases.map(parseRelease))
37+
if (moduleConfig.parseContents) {
38+
releases = await Promise.all(releases.map(release => parseRelease(release, githubConfig)))
3839
}
3940

4041
// Sort DESC by release version or date

src/runtime/server/api/releases/last.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import { useQuery } from 'h3'
12
import { fetchReleases, overrideConfig, parseRelease } from '../../utils/queries'
23
import type { ModuleOptions } from '../../../../module'
34
import { GithubRawRelease, GithubRepositoryOptions } from '../../../types'
5+
// @ts-ignore
46
import * as imports from '#imports'
57

68
let handler
@@ -29,8 +31,8 @@ export default handler(
2931
// Fetches releases from GitHub
3032
let release = await fetchReleases({ last: true }, githubConfig)
3133

32-
if (release && moduleConfig.parseReleases) {
33-
release = await parseRelease(release as GithubRawRelease)
34+
if (release && moduleConfig.parseContents) {
35+
release = await parseRelease(release as GithubRawRelease, githubConfig)
3436
}
3537

3638
return release

src/runtime/server/api/repository.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import { useQuery } from 'h3'
12
import { fetchRepository, overrideConfig } from '../utils/queries'
23
import { ModuleOptions } from '../../../module'
34
import { GithubRepository, GithubRepositoryOptions } from '../../types'
5+
// @ts-ignore
46
import * as imports from '#imports'
57

68
let handler

src/runtime/server/utils/queries.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import type {
55
ModuleOptions
66
} from '../../../module'
77
import { GithubRawRelease, GithubRepositoryOptions, GithubRawContributors, GithubContributorsQuery, GithubReleasesQuery, GithubRepositoryReadme, GithubRepository } from '../../types'
8+
// @ts-ignore
89
import { parseContent } from '#content/server'
10+
911
function isBot (user) {
1012
return user.login.includes('[bot]') || user.login.includes('-bot') || user.login.includes('.bot')
1113
}
@@ -54,11 +56,19 @@ export function githubGraphqlQuery<T = any> (query: string, options: Partial<Mod
5456
return gq<T>(query)
5557
}
5658

57-
export const parseRelease = async (release: GithubRawRelease) => {
59+
export const parseRelease = async (release: GithubRawRelease, githubConfig: GithubRepositoryOptions) => {
5860
return {
5961
...release,
6062
// Parse release notes when `@nuxt/content` is installed.
61-
...(typeof parseContent === 'function' && release?.body && release?.name ? await parseContent(`github:${release.name}.md`, release.body) : {})
63+
...(typeof parseContent === 'function' && release?.body && release?.name ? await parseContent(`github:${release.name}.md`, release.body) : {}, {
64+
markdown: {
65+
remarkPlugins: {
66+
'remark-github': {
67+
repository: `${githubConfig.owner}/${githubConfig.repo}`
68+
}
69+
}
70+
}
71+
})
6272
}
6373
}
6474

@@ -77,15 +87,12 @@ export async function fetchRepository ({ api, owner, repo, token }: GithubReposi
7787
}
7888
}).catch((_) => {
7989
/*
80-
8190
// eslint-disable-next-line no-console
8291
console.warn(`Cannot fetch GitHub Repository on ${url} [${err.response?.status || 500}]`)
8392
8493
// eslint-disable-next-line no-console
8594
console.info('If your repository is private, make sure to provide GITHUB_TOKEN environment in `.env`')
86-
8795
*/
88-
8996
return {}
9097
})
9198

@@ -103,7 +110,6 @@ export async function fetchRepositoryContributors ({ max }: Partial<GithubContri
103110
}
104111
}).catch((_) => {
105112
/*
106-
107113
// eslint-disable-next-line no-console
108114
console.warn(`Cannot fetch GitHub contributors on ${url} [${err.response?.status || 500}]`)
109115
@@ -114,9 +120,7 @@ export async function fetchRepositoryContributors ({ max }: Partial<GithubContri
114120
// eslint-disable-next-line no-console
115121
console.info('To disable fetching contributors, set `github.contributors` to `false` in `nuxt.config.ts`')
116122
}
117-
118123
*/
119-
120124
return []
121125
})
122126

@@ -151,7 +155,6 @@ export async function fetchFileContributors ({ source, max }: Partial<GithubCont
151155
{ token }
152156
).catch((_) => {
153157
/*
154-
155158
// eslint-disable-next-line no-console
156159
console.warn(`Cannot fetch GitHub file contributors on ${source} [${err.response?.status || 500}]`)
157160
@@ -162,7 +165,6 @@ export async function fetchFileContributors ({ source, max }: Partial<GithubCont
162165
// eslint-disable-next-line no-console
163166
console.info('To disable fetching contributors, set `github.contributors` to `false` in `nuxt.config.ts`')
164167
}
165-
166168
*/
167169
})
168170

@@ -202,7 +204,6 @@ export async function fetchReleases (query: Partial<GithubReleasesQuery>, { api,
202204
}
203205
}).catch((_) => {
204206
/*
205-
206207
// eslint-disable-next-line no-console
207208
console.warn(`Cannot fetch GitHub releases on ${url} [${err.response?.status || 500}]`)
208209
@@ -213,7 +214,6 @@ export async function fetchReleases (query: Partial<GithubReleasesQuery>, { api,
213214
// eslint-disable-next-line no-console
214215
console.info('To disable fetching releases, set `github.releases` to `false` in `nuxt.config.ts`')
215216
}
216-
217217
*/
218218
})
219219

@@ -233,13 +233,11 @@ export async function fetchReadme ({ api, owner, repo, token }: GithubRepository
233233
}
234234
}).catch((_) => {
235235
/*
236-
237236
// eslint-disable-next-line no-console
238237
console.warn(`Cannot fetch GitHub readme on ${url} [${err.response?.status || 500}]`)
239238
240239
// eslint-disable-next-line no-console
241240
console.info('If your repository is private, make sure to provide GITHUB_TOKEN environment in `.env`')
242-
243241
*/
244242

245243
return {}

0 commit comments

Comments
 (0)