Skip to content

Commit

Permalink
feat: better helpers
Browse files Browse the repository at this point in the history
Signed-off-by: Neko Ayaka <[email protected]>
  • Loading branch information
nekomeowww committed Jan 20, 2024
1 parent 37ae485 commit bf371ff
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
40 changes: 35 additions & 5 deletions packages/vitepress-plugin-page-properties/src/vite/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
import { env } from 'node:process'
import { relative } from 'node:path'
import type { Plugin } from 'vite'
import GrayMatter from 'gray-matter'
import { pathEndsWith, pathEquals, pathStartsWith } from './path'

export interface Context {
helpers: {
pathEquals: (path: string, equalsWith: string) => boolean
pathStartsWith: (path: string, startsWith: string) => boolean
pathEndsWith: (path: string, endsWith: string) => boolean
idEquals: (equalsWith: string) => boolean
idStartsWith: (startsWith: string) => boolean
idEndsWith: (endsWith: string) => boolean
}
}

export function PagePropertiesMarkdownSection(options?: {
getPagePropertiesTitle?: (code: string, id: string) => string
getPagePropertiesTitle?: (code: string, id: string, context: Context) => string
excludes?: string[]
exclude?: (id: string) => boolean
exclude?: (id: string, context: Context) => boolean
}): Plugin {
const {
getPagePropertiesTitle = () => {
Expand All @@ -15,19 +28,36 @@ export function PagePropertiesMarkdownSection(options?: {
exclude = () => false,
} = options ?? {}

let root = ''

return {
name: '@nolebase/vitepress-plugin-page-properties-markdown-section',
configResolved(config) {
root = config.root ?? ''
},
transform(code, id) {
function idEndsWith(endsWith) {
return pathEndsWith(relative(root, id), endsWith)
}

function idEquals(equals) {
return pathEquals(relative(root, id), equals)
}

function idStartsWith(startsWith) {
return pathStartsWith(relative(root, id), startsWith)
}

if (!id.endsWith('.md'))
return null
if (excludes.includes(id))
return null
if (exclude(id))
if (exclude(id, { helpers: { pathEndsWith, pathEquals, pathStartsWith, idEndsWith, idEquals, idStartsWith } }))
return null

const targetComponent = env.NODE_ENV === 'development'
? `<div class="mt-4" />\n\n\n##### ${getPagePropertiesTitle(code, id)}\n\n<NolebasePagePropertiesEditor />\n\n\n<div class="mb-4" />`
: `<div class="mt-4" />\n\n\n##### ${getPagePropertiesTitle(code, id)}\n\n<NolebasePageProperties />\n\n\n<div class="mb-4" />`
? `<div class="mt-4" />\n\n\n##### ${getPagePropertiesTitle(code, id, { helpers: { pathEndsWith, pathEquals, pathStartsWith, idEndsWith, idEquals, idStartsWith } })}\n\n<NolebasePagePropertiesEditor />\n\n\n<div class="mb-4" />`
: `<div class="mt-4" />\n\n\n##### ${getPagePropertiesTitle(code, id, { helpers: { pathEndsWith, pathEquals, pathStartsWith, idEndsWith, idEquals, idStartsWith } })}\n\n<NolebasePageProperties />\n\n\n<div class="mb-4" />`

const parsedMarkdownContent = GrayMatter(code)
const hasFrontmatter = Object.keys(parsedMarkdownContent.data).length > 0
Expand Down
20 changes: 20 additions & 0 deletions packages/vitepress-plugin-page-properties/src/vite/path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { sep } from 'node:path'

export function normalizePath(path: string): string {
if (sep === '/')
return path

return path.split(sep).join('/')
}

export function pathEquals(path: string, equals: string): boolean {
return normalizePath(path) === (normalizePath(equals))
}

export function pathStartsWith(path: string, startsWith: string): boolean {
return normalizePath(path).startsWith(normalizePath(startsWith))
}

export function pathEndsWith(path: string, startsWith: string): boolean {
return normalizePath(path).endsWith(normalizePath(startsWith))
}

0 comments on commit bf371ff

Please sign in to comment.