Skip to content

Commit 55d45f3

Browse files
committed
now also able to render changelog markdown files
1 parent 34faad0 commit 55d45f3

File tree

8 files changed

+82
-10
lines changed

8 files changed

+82
-10
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<script setup lang="ts">
2+
const { info } = defineProps<{ info: ChangelogMarkdownInfo }>()
3+
4+
const { data } = useLazyFetch(() => `/api/changelog/md/${info.provider}/${info.repo}/${info.path}`)
5+
</script>
6+
<template>
7+
<div v-if="data?.toc && data.toc.length > 1" class="flex justify-end mt-3">
8+
<ReadmeTocDropdown :toc="data.toc" class="justify-self-end" />
9+
</div>
10+
<Readme v-if="data?.html" :html="data.html"></Readme>
11+
</template>

app/pages/package-changes/[...path].vue

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ watch(
6363
6464
// getting info
6565
66-
const { data: changelog } = usePackageChangelog(packageName, version)
66+
const { data: changelog, pending } = usePackageChangelog(packageName, version)
6767
</script>
6868
<template>
6969
<main class="flex-1 flex flex-col">
@@ -92,7 +92,11 @@ const { data: changelog } = usePackageChangelog(packageName, version)
9292

9393
<section class="container w-full" v-if="changelog">
9494
<LazyChangelogReleases v-if="changelog.type == 'release'" :info="changelog" />
95-
<p v-else>changelog.md support is comming or the package doesn't have changelogs</p>
95+
<LazyChangelogMarkdown
96+
v-else-if="changelog.type == 'md'"
97+
:info="changelog"
98+
></LazyChangelogMarkdown>
99+
<p v-else-if="!pending">Sorry, this package doesn't track any changelogs</p>
96100
</section>
97101
</main>
98102
</template>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import * as v from 'valibot'
2+
import {
3+
ERROR_CHANGELOG_FILE_FAILED,
4+
ERROR_THROW_INCOMPLETE_PARAM,
5+
} from '~~/shared/utils/constants'
6+
7+
export default defineCachedEventHandler(async event => {
8+
const provider = getRouterParam(event, 'provider')
9+
const repo = getRouterParam(event, 'repo')
10+
const owner = getRouterParam(event, 'owner')
11+
const path = getRouterParam(event, 'path')
12+
13+
if (!repo || !provider || !owner || !path) {
14+
throw createError({
15+
status: 404,
16+
statusMessage: ERROR_THROW_INCOMPLETE_PARAM,
17+
})
18+
}
19+
20+
try {
21+
console.log({ provider })
22+
23+
switch (provider as ProviderId) {
24+
case 'github':
25+
return await getGithubMarkDown(owner, repo, path)
26+
27+
default:
28+
throw createError({
29+
status: 404,
30+
statusMessage: ERROR_CHANGELOG_NOT_FOUND,
31+
})
32+
}
33+
} catch (error) {
34+
handleApiError(error, {
35+
statusCode: 502,
36+
message: ERROR_CHANGELOG_FILE_FAILED,
37+
})
38+
}
39+
})
40+
41+
async function getGithubMarkDown(owner: string, repo: string, path: string) {
42+
const data = await $fetch(`https://ungh.cc/repos/${owner}/${repo}/files/HEAD/${path}`)
43+
44+
const markdown = v.parse(v.string(), data)
45+
46+
return (await changelogRenderer())(markdown)
47+
}

server/api/changelog/releases/[provider]/[owner]/[repo].ts renamed to server/api/changelog/releases/[provider]/[owner]/[repo].get.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import type { ProviderId } from '~~/shared/utils/git-providers'
22
import type { ReleaseData } from '~~/shared/types/changelog'
3-
import { ERROR_CHANGELOG_RELEASES_FAILED, THROW_INCOMPLETE_PARAM } from '~~/shared/utils/constants'
3+
import {
4+
ERROR_CHANGELOG_RELEASES_FAILED,
5+
ERROR_THROW_INCOMPLETE_PARAM,
6+
} from '~~/shared/utils/constants'
47
import { GithubReleaseCollectionSchama } from '~~/shared/schemas/changelog/release'
58
import { parse } from 'valibot'
69
import { changelogRenderer } from '~~/server/utils/changelog/markdown'
@@ -13,7 +16,7 @@ export default defineCachedEventHandler(async event => {
1316
if (!repo || !provider || !owner) {
1417
throw createError({
1518
status: 404,
16-
statusMessage: THROW_INCOMPLETE_PARAM,
19+
statusMessage: ERROR_THROW_INCOMPLETE_PARAM,
1720
})
1821
}
1922

@@ -23,12 +26,14 @@ export default defineCachedEventHandler(async event => {
2326
return await getReleasesFromGithub(owner, repo)
2427

2528
default:
26-
return false
29+
throw createError({
30+
status: 404,
31+
statusMessage: ERROR_CHANGELOG_NOT_FOUND,
32+
})
2733
}
2834
} catch (error) {
2935
handleApiError(error, {
3036
statusCode: 502,
31-
// message: 'temp',
3237
message: ERROR_CHANGELOG_RELEASES_FAILED,
3338
})
3439
}

server/utils/changelog/detectChangelog.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ async function checkChangelogFile(ref: RepoRef): Promise<ChangelogMarkdownInfo |
107107
type: 'md',
108108
provider: ref.provider,
109109
path: fileName,
110+
repo: `${ref.owner}/${ref.repo}`,
110111
} satisfies ChangelogMarkdownInfo
111112
}
112113
}

server/utils/changelog/markdown.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export async function changelogRenderer() {
5454
</div>`
5555
}
5656

57-
return (markdown: string | null, releaseId: string | number) => {
57+
return (markdown: string | null, releaseId?: string | number) => {
5858
// Collect table of contents items during parsing
5959
const toc: TocItem[] = []
6060

@@ -68,7 +68,7 @@ export async function changelogRenderer() {
6868
// Track used heading slugs to handle duplicates (GitHub-style: foo, foo-1, foo-2)
6969
const usedSlugs = new Map<string, number>()
7070

71-
let lastSemanticLevel = 2 // Start after h2 (the "Readme" section heading)
71+
let lastSemanticLevel = releaseId ? 2 : 1 // Start after h2 (the "Readme" section heading)
7272
renderer.heading = function ({ tokens, depth }: Tokens.Heading) {
7373
// Calculate the target semantic level based on document structure
7474
// Start at h3 (since page h1 + section h2 already exist)
@@ -89,7 +89,9 @@ export async function changelogRenderer() {
8989

9090
// Prefix with 'user-content-' to avoid collisions with page IDs
9191
// (e.g., #install, #dependencies, #versions are used by the package page)
92-
const id = `user-content-${releaseId}-${uniqueSlug}`
92+
const id = releaseId
93+
? `user-content-${releaseId}-${uniqueSlug}`
94+
: `user-content-${uniqueSlug}`
9395

9496
// Collect TOC item with plain text (HTML stripped)
9597
const plainText = text

shared/types/changelog.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export interface ChangelogMarkdownInfo {
1414
* location within the repository
1515
*/
1616
path: string
17+
repo: `${string}/${string}`
1718
}
1819

1920
export type ChangelogInfo = ChangelogReleaseInfo | ChangelogMarkdownInfo

shared/utils/constants.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ export const ERROR_NEED_REAUTH = 'User needs to reauthenticate'
3838
export const ERROR_CHANGELOG_NOT_FOUND =
3939
'No releases or changelogs have been found for this package'
4040
export const ERROR_CHANGELOG_RELEASES_FAILED = 'Failed to get releases'
41-
export const THROW_INCOMPLETE_PARAM = "Couldn't do request due to incomplete parameters"
41+
export const ERROR_CHANGELOG_FILE_FAILED = 'Failed to get changelog markdown'
42+
export const ERROR_THROW_INCOMPLETE_PARAM = "Couldn't do request due to incomplete parameters"
4243

4344
// microcosm services
4445
export const CONSTELLATION_HOST = 'constellation.microcosm.blue'

0 commit comments

Comments
 (0)