Skip to content

Commit

Permalink
feat: support frontmatter preprocess
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jan 31, 2021
1 parent ee316bf commit 959842f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
33 changes: 33 additions & 0 deletions src/head.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { ResolvedOptions } from './types'

export function preprocessHead(frontmatter: any, options: ResolvedOptions) {
if (!options.headEnabled)
return frontmatter

const head = options.headField ? frontmatter.head || {} : frontmatter

const meta = head.meta = head.meta || []

if (head.title) {
if (!meta.find((i: any) => i.property === 'og:title'))
meta.push({ property: 'og:title', content: head.title })
}

if (head.description) {
if (!meta.find((i: any) => i.property === 'og:description'))
meta.push({ property: 'og:description', content: head.description })

if (!meta.find((i: any) => i.name === 'description'))
meta.push({ name: 'description', content: head.description })
}

if (head.image) {
if (!meta.find((i: any) => i.property === 'og:image'))
meta.push({ property: 'og:image', content: head.image })

if (!meta.find((i: any) => i.property === 'twitter:card'))
meta.push({ name: 'twitter:card', content: 'summary_large_image' })
}

return frontmatter
}
6 changes: 4 additions & 2 deletions src/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ export function createMarkdown(options: ResolvedOptions) {
options.markdownItSetup(markdown)

return (id: string, raw: string) => {
const { wrapperClasses, wrapperComponent, transforms, headEnabled, headField } = options
const { wrapperClasses, wrapperComponent, transforms, headEnabled, headField, frontmatterPreprocess } = options

if (transforms.before)
raw = transforms.before(raw, id)

const { content: md, data: frontmatter } = matter(raw)
const { content: md, data } = matter(raw)
const frontmatter = frontmatterPreprocess(data, options)

let html = markdown.render(md, {})

if (wrapperClasses)
Expand Down
2 changes: 2 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { preprocessHead } from './head'
import { Options, ResolvedOptions } from './types'
import { toArray } from './utils'

Expand All @@ -11,6 +12,7 @@ export function resolveOptions(userOptions: Options): ResolvedOptions {
wrapperClasses: 'markdown-body',
wrapperComponent: null,
transforms: {},
frontmatterPreprocess: preprocessHead,
}, userOptions) as ResolvedOptions

options.wrapperClasses = toArray(options.wrapperClasses).filter(i => i).join(' ')
Expand Down
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-use-before-define */
import type MarkdownIt from 'markdown-it'

export interface Options {
Expand All @@ -15,6 +16,12 @@ export interface Options {
* @default ''
*/
headField?: string

/**
* Custom function to process the frontmatter
*/
frontmatterPreprocess?: (frontmatter: any, options: ResolvedOptions) => any

/**
* Options passed to Markdown It
*/
Expand Down

0 comments on commit 959842f

Please sign in to comment.