vitepress icon indicating copy to clipboard operation
vitepress copied to clipboard

Add `updatedCommitHash` to the `page` object

Open onmax opened this issue 1 year ago • 6 comments

Is your feature request related to a problem? Please describe.

I would like to show a text in the footer like:

Edited at ${updatedAt} (${commitHash}).

Describe the solution you'd like

updatedAt is being fetched from this getGitTimestamp function.

I would like to propose a new function called getCommitHash function which also receives a file: string as an argument called updatedCommitHash and then add it to the page type

Describe alternatives you've considered

I am going to built this code for my own vitepress project, but I think it might be beneficial for other pepole.

Additional context

I would like to make a PR if we agree this is a good thing to add to vitepress.

First, I would like to hear an OK from the maintainers.

PD: Suggest better names as I think my proposed names are not very descriptive 😄

Validations

onmax avatar Jan 31 '24 04:01 onmax

You can just add it using transformPageData instead 🤷 Do module augmentation if you need types too.

brc-dd avatar Jan 31 '24 05:01 brc-dd

I have implemented my solution here:

https://github.com/onmax/nimiq-developer-center/commit/7a8364d0f32cef50880e1a99ec3560c95ebda2d2

But I think is nice to have from vitepress. Maybe other people need :/

As a side note, it would be also awesome to add documentation on how to extend the PageData when using the transformPageData function

onmax avatar Jan 31 '24 05:01 onmax

Maybe other people need

So far, you're the first to request this. So I'll maybe wait for more people to upvote this feature request. Keeping it open for now. If there's significant demand we can consider adding it.

how to extend the PageData when using the transformPageData function

Do you mean the types? You can do something like this (refer https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation):

declare module 'vitepress' {
  interface PageData {
    updatedCommitHash: string
  }
}

brc-dd avatar Jan 31 '24 05:01 brc-dd

Thank you!

I have added the code you sent me like this in the root (view it in GitHub)

/// <reference path="node_modules/vitepress/types/index.d.ts" />

// rewrite the PageData type from 'vitepress' to add the field `updatedCommitHash`
declare module 'vitepress' {
  export interface PageData {
    updatedCommitHash?: number
  }
}

But I still see an error:

See error

image

Also tried reloading VSCode but it keeps showing me the error

onmax avatar Jan 31 '24 05:01 onmax

You'll need to add the files to include array in tsconfig too. Also triple slash reference is not needed, you can do something like

// vitepress-extension.d.ts

declare module 'vitepress' {
  interface PageData {
    updatedCommitHash: string
  }
}

export {}
// in tsconfig

  "include": ["vitepress-extension.d.ts", ".vitepress/**/*"]

brc-dd avatar Jan 31 '24 05:01 brc-dd

Thanks!! That worked!

onmax avatar Jan 31 '24 05:01 onmax