Skip to content

Commit

Permalink
feat: Extract git log metadata about content markdown files (#14)
Browse files Browse the repository at this point in the history
* feat: Extract git log metadata about content markdown files

* chore: remove unnecessary .gitignore
  • Loading branch information
holloway authored Nov 12, 2024
1 parent 136de90 commit 260af2f
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 12 deletions.
3 changes: 3 additions & 0 deletions client/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ logs
.env
.env.*
!.env.example

# generated by generate-content-metadata.ts
content-metadata.json
111 changes: 101 additions & 10 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
"test:story": "LOST_PIXEL_DISABLE_TELEMETRY=1 npx --no lost-pixel docker",
"test:story:view": "npx --no -c 'vite dev ./visual-regression-viewer'",
"test:story:approve": "LOST_PIXEL_DISABLE_TELEMETRY=1 npx --no lost-pixel docker update",
"types": "npx nuxi typecheck"
"types": "npx nuxi typecheck",
"generate-content-metadata": "node --experimental-strip-types ./scripts/generate-content-metadata.ts"
},
"//": "If updating lost-pixel sync the version to the vis-reg-test.yml file",
"//": "If updating lost-pixel sync the version to the vis-reg-test.yml file or rendering / filenames generated etc may be different",
"dependencies": {
"@ark-ui/vue": "^3.13.0",
"@nuxt/content": "^2.13.4",
Expand All @@ -30,13 +31,15 @@
"@pinia/nuxt": "^0.5.4",
"@playwright/test": "^1.40.0",
"@tailwindcss/forms": "^0.5.8",
"glob": "^11.0.0",
"lodash-es": "^4.17.21",
"lost-pixel": "3.18.2",
"luxon": "^3.5.0",
"nuxt": "^3.13.1",
"nuxt-headlessui": "^1.2.0",
"pinia": "^2.2.2",
"playwright": "^1.47.1",
"simple-git": "^3.27.0",
"vue": "^3.5.4"
},
"devDependencies": {
Expand Down
14 changes: 14 additions & 0 deletions client/pages/[...slug].vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,33 @@
<div class="min-h-[100vh]">
<div class="container mx-auto">
<Breadcrumbs :breadcrumb-items="breadcrumbItems" />
<pre>
{{ route.path }}
{{ thisContentMetadata }}
</pre>
<ContentDoc />
</div>
</div>
</template>

<script setup lang="ts">
import { startCase } from 'lodash-es'
import _contentMetadata from '../content-metadata.json'
import type { BreadcrumbItem } from '~/components/BreadcrumbsTypes'
import type { ContentMetadata } from '~/scripts/generate-content-metadata'
const contentMetadata: ContentMetadata = _contentMetadata
const route = useRoute()
const thisContentMetadata = contentMetadata[route.path]
const pathParts = route.path.substring(1).split('/')
// const { data: navigation } = await useAsyncData('navigation', () =>
// fetchContentNavigation()
// )
const breadcrumbItems: BreadcrumbItem[] = [
{ url: '/', label: 'Home' },
...pathParts.map((pathPart, index, arr) => ({
Expand Down
57 changes: 57 additions & 0 deletions client/scripts/generate-content-metadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import path from 'node:path'
import fs from 'node:fs'

import { simpleGit } from 'simple-git'
import { glob } from 'glob'

const __dirname = import.meta.dirname
const clientPath = path.resolve(__dirname, '..')
const contentPath = path.resolve(clientPath, 'content')

const contentMarkdownPaths = await glob(path.join(contentPath, '**/*.md'))

const git = simpleGit()

export type ContentMetadata = Record<
string, // path within content directory
{
timestamp: string // timestamp ISO 8601
}
>

const markdownMetadataArray = await Promise.all(
contentMarkdownPaths.map(
(contentMarkdownPath) =>
new Promise<ContentMetadata>((resolve, reject) => {
git
.log({
file: contentMarkdownPath,
maxCount: 1,
strictDate: true
})
.then((gitLog) => {
if (gitLog.latest?.date) {
const relativePath = contentMarkdownPath.substring(contentPath.length).replace(/\.md$/, '')
resolve({
[relativePath]: { timestamp: gitLog.latest?.date }
})
} else {
reject(
`Unable to extract latest Git log time from path ${contentMarkdownPath}`
)
}
})
})
)
)

const contentMetadata: ContentMetadata = Object.assign({}, ...markdownMetadataArray)

const contentMetadataPath = path.join(clientPath, 'content-metadata.json')

fs.writeFileSync(contentMetadataPath, JSON.stringify(contentMetadata, null, 2))

console.log(
'Success: timestamps extract from git and written to',
contentMetadataPath
)

0 comments on commit 260af2f

Please sign in to comment.