1.0.0-rc.56 • Published 3 days ago

@vuepress-plume/plugin-auto-frontmatter v1.0.0-rc.56

Weekly downloads
-
License
MIT
Repository
github
Last release
3 days ago

@vuepress-plume/plugin-auto-frontmatter

自动生成 *.md 文件的 frontmatter 配置。

Install

npm install @vuepress-plume/plugin-auto-frontmatter
# or
pnpm add @vuepress-plume/plugin-auto-frontmatter
# or
yarn add @vuepress-plume/plugin-auto-frontmatter

Usage

// .vuepress/config.[jt]s
import { autoFrontmatterPlugin } from '@vuepress-plume/plugin-auto-frontmatter'

export default {
  // ...
  plugins: [
    autoFrontmatterPlugin({
      formatter: {
        createTime(formatTime, file, matter) {
          if (formatTime)
            return formatTime
          return file.createTime
        }
      }
    })
  ]
  // ...
}

autoFrontmatterPlugin([options])

options

{ include?: string | string[]; exclude?: string | string[]; formatter: Formatter }

  • include include 匹配字符串或数组,匹配需要自动生成 frontmatter 的 md文件。 默认预设为 ['**/*.md']

  • exclude exclude 排除不需要的文件 默认预设为: ['!.vuepress/', '!node_modules/']

  • formatter 配置frontmatter每个字段的生成规则。

    interface MarkdownFile {
      filepath: string
      relativePath: string
      content: string
      createTime: Date
      stats: fs.Stats
    }
    
    interface FormatterFn<T = any, K = object> {
      (value: T, file: MarkdownFile, data: K): T
    }
    
    type FormatterObject<K = object, T = any> = Record<
      string,
      FormatterFn<T, K>
    >
    
    type FormatterArray = {
      include: string | string[]
      formatter: FormatterObject
    }[]
    
    type Formatter = FormatterObject | FormatterArray
    
    /**
     * formatterObj 对象中的 key 即为 frontmatter 配置中的key
     * 其方法返回的值将作为 frontmatter[key] 的值
     * .md
     * ---
     * createTime: 2022-03-26T11:46:50.000Z
     * ---
     */
    const formatterObj: Formatter = {
      createTime(formatTime, file, matter) {
        if (formatTime)
          return formatTime
        return file.createTime
      }
    }
    
    const formatterArr: Formatter = [
      {
        // 更精细化的匹配某个 md文件,支持glob 匹配字符串
        include: '**/{README,index}.md',
        // formatter 仅对 glob命中的文件有效
        formatter: {
          home(value, file, matter) {
            return value
          }
        },
      },
      {
        // 通配,如果文件没有被其他精细glob命中,
        // 则使用 通配 formatter
        // 如果是数组,必须有且用一个 include 为 * 的 项
        include: '*',
        formatter: {
          title(title) {
            return title || '默认标题'
          }
        }
      }
    ]

Why ?

  • 为什么需要这个插件?

    有时候在开发一些主题时,期望使用户更专注于内容的编写,尽可能减少配置性的工作,可以将一些重复性的必要的配置 直接通过本插件自动生成。

1.0.0-rc.56

3 days ago

1.0.0-rc.55

8 days ago

1.0.0-rc.54

14 days ago

1.0.0-rc.53

22 days ago

1.0.0-rc.52

26 days ago

1.0.0-rc.51

28 days ago

1.0.0-rc.50

28 days ago

1.0.0-rc.49

1 month ago

1.0.0-rc.48

1 month ago

1.0.0-rc.47

2 months ago

1.0.0-rc.46

2 months ago

1.0.0-rc.45

2 months ago

1.0.0-rc.44

2 months ago

1.0.0-rc.43

2 months ago

1.0.0-rc.42

2 months ago

1.0.0-rc.41

2 months ago

1.0.0-rc.40

2 months ago

1.0.0-rc.39

3 months ago

1.0.0-rc.38

3 months ago

1.0.0-rc.37

3 months ago

1.0.0-rc.36

3 months ago

1.0.0-rc.35

3 months ago

1.0.0-rc.34

3 months ago

1.0.0-rc.33

3 months ago

1.0.0-rc.32

3 months ago

1.0.0-rc.31

3 months ago

1.0.0-rc.30

3 months ago

1.0.0-rc.29

4 months ago

1.0.0-rc.28

4 months ago

1.0.0-rc.27

4 months ago

1.0.0-rc.26

4 months ago

1.0.0-rc.25

4 months ago

1.0.0-rc.24

4 months ago

1.0.0-rc.23

4 months ago

1.0.0-rc.20

4 months ago

1.0.0-rc.22

4 months ago

1.0.0-rc.21

4 months ago

1.0.0-rc.17

4 months ago

1.0.0-rc.16

4 months ago

1.0.0-rc.19

4 months ago

1.0.0-rc.18

4 months ago

1.0.0-rc.15

4 months ago

1.0.0-rc.14

4 months ago

1.0.0-rc.13

4 months ago

1.0.0-rc.12

4 months ago

1.0.0-rc.11

4 months ago

1.0.0-rc.9

4 months ago

1.0.0-rc.8

5 months ago

1.0.0-rc.10

4 months ago

1.0.0-rc.7

5 months ago

1.0.0-rc.5

5 months ago

1.0.0-rc.6

5 months ago

1.0.0-rc.4

5 months ago

1.0.0-rc.3

5 months ago

1.0.0-rc.2

5 months ago

1.0.0-rc.1

5 months ago

1.0.0-rc.0

5 months ago

1.0.0-beta.89

5 months ago