Skip to content

Commit

Permalink
fix(bi-directional-links|og-image): replaced fast-glob with glob with…
Browse files Browse the repository at this point in the history
… better esm support and compatibilities (#130)

Signed-off-by: Neko Ayaka <[email protected]>
  • Loading branch information
nekomeowww authored Mar 27, 2024
1 parent a98f2e0 commit 087ae0f
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 80 deletions.
2 changes: 1 addition & 1 deletion packages/markdown-it-bi-directional-links/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
},
"dependencies": {
"colorette": "^2.0.20",
"fast-glob": "^3.3.2"
"glob": "^10.3.10"
},
"devDependencies": {
"@types/markdown-it": "^13.0.7",
Expand Down
36 changes: 16 additions & 20 deletions packages/markdown-it-bi-directional-links/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { basename, extname, posix, relative, sep } from 'node:path'
import { env } from 'node:process'
import fg from 'fast-glob'
import { globSync } from 'glob'
import type { PluginSimple } from 'markdown-it'
import { cyan, gray, yellow } from 'colorette'

Expand All @@ -18,15 +18,20 @@ function logIncorrectMatchedMarkupWarning(
src: string,
path: string,
) {
const debug = (import.meta?.env?.DEBUG ?? env?.DEBUG) || ''
const debug = (env?.DEBUG) ?? ''

if (!debug)
return
if (debug !== '@nolebase/*')
return
if (debug !== '@nolebase/markdown-it-*')
return
if (!debug.includes('@nolebase/markdown-it-bi-directional-links'))

let shouldLog = false
if (debug === '@nolebase/*')
shouldLog = true
if (debug === '@nolebase/markdown-it-*')
shouldLog = true
if (debug === '@nolebase/markdown-it-bi-directional-links')
shouldLog = true

if (!shouldLog)
return

console.warn(`${yellow(`[@nolebase/markdown-it-bi-directional-links] [WARN] Matched markup '`) + input + yellow(`' is not at the start of the text.`)} ${yellow(`
Expand Down Expand Up @@ -62,10 +67,6 @@ Matching chain:
`)
}

function trimInvalidCharsForFileName(str: string) {
return str.replace(/`/g, '')
}

/**
* A markdown-it plugin to support bi-directional links.
* @param options - Options.
Expand All @@ -91,8 +92,8 @@ export const BiDirectionalLinks: (options: {
}

for (const include of includes) {
const files = fg.sync(include, {
onlyFiles: true,
const files = globSync(include, {
nodir: true,
absolute: true,
cwd: rootDir,
ignore: [
Expand Down Expand Up @@ -169,11 +170,6 @@ export const BiDirectionalLinks: (options: {
if (!isImageRef && extname(osSpecificHref) === '')
osSpecificHref += '.md'

// before matching against actual file path, we will need to normalize
// the osSpecificHref without any invalid characters for file systems,
// e.g. ` (backtick) is not allowed.
osSpecificHref = trimInvalidCharsForFileName(basename(osSpecificHref))

const matchedHref = findBiDirectionalLinks(possibleBiDirectionalLinksInCleanBaseNameOfFilePaths, possibleBiDirectionalLinksInFullFilePaths, osSpecificHref)
if (!matchedHref) {
logNoMatchedFileWarning(inputContent, markupTextContent, href, osSpecificHref, state.env.path)
Expand All @@ -183,9 +179,9 @@ export const BiDirectionalLinks: (options: {
const resolvedNewHref = posix.join(options.baseDir ?? '/', relative(rootDir, matchedHref).split(sep).join('/'))

if (isImageRef)
genImage(state, resolvedNewHref, link, text)
genImage(state, resolvedNewHref, text, link)
else
genLink(state, resolvedNewHref, link, text)
genLink(state, resolvedNewHref, text, md, href, link)

return true
})
Expand Down
35 changes: 18 additions & 17 deletions packages/markdown-it-bi-directional-links/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { sep } from 'node:path'
import type Token from 'markdown-it/lib/token'
import type StateInline from 'markdown-it/lib/rules_inline/state_inline'
import type MarkdownIt from 'markdown-it'

/**
* Find / resolve bi-directional links.
Expand All @@ -22,22 +23,7 @@ export function findBiDirectionalLinks(
return possibleBiDirectionalLinksInFilePaths[href]
}

export function genImage(state: StateInline, resolvedNewHref: string, link: string[], text: string) {
const openToken = state.push('image', 'img', 1)
openToken.attrSet('src', resolvedNewHref)
openToken.attrSet('alt', '')

openToken.children = []
openToken.content = text

const innerTextToken = state.push('text', '', 0)
innerTextToken.content = text
openToken.children.push(innerTextToken)

state.pos += link![0].length
}

export function genLink(state: StateInline, resolvedNewHref: string, link: string[], text: string) {
export function genLink(state: StateInline, resolvedNewHref: string, text: string, md: MarkdownIt, href: string, link: RegExpMatchArray) {
// Create new link_open
const openToken = state.push('link_open', 'a', 1)
openToken.attrSet('href', resolvedNewHref)
Expand All @@ -46,7 +32,7 @@ export function genLink(state: StateInline, resolvedNewHref: string, link: strin
const linkTokenChildrenContent: Token[] = []

// Produces a set of inline tokens and each contains a set of children tokens
const parsedInlineTokens = text ? state.md.parseInline(text, state.env) : state.md.parseInline(resolvedNewHref, state.env) || []
const parsedInlineTokens = text ? md.parseInline(text, state.env) : md.parseInline(href, state.env) || []

// We are going to push the children tokens of each inline token to the final inline tokens
// Need to check if the parsed inline tokens have children tokens
Expand Down Expand Up @@ -74,3 +60,18 @@ export function genLink(state: StateInline, resolvedNewHref: string, link: strin
// Update the position in the source string
state.pos += link![0].length
}

export function genImage(state: StateInline, resolvedNewHref: string, text: string, link: RegExpMatchArray) {
const openToken = state.push('image', 'img', 1)
openToken.attrSet('src', resolvedNewHref)
openToken.attrSet('alt', '')

openToken.children = []
openToken.content = text

const innerTextToken = state.push('text', '', 0)
innerTextToken.content = text
openToken.children.push(innerTextToken)

state.pos += link![0].length
}
2 changes: 1 addition & 1 deletion packages/vitepress-plugin-og-image/build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default defineBuildConfig({
'vite',
'vitepress',
'fs-extra',
'fast-glob',
'glob',
'emoji-regex',
'@resvg/resvg-js',
'gray-matter',
Expand Down
2 changes: 1 addition & 1 deletion packages/vitepress-plugin-og-image/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@
"@resvg/resvg-js": "^2.6.0",
"colorette": "^2.0.20",
"emoji-regex": "^10.3.0",
"fast-glob": "^3.3.2",
"fs-extra": "^11.2.0",
"glob": "^10.3.10",
"gray-matter": "^4.0.3",
"ora": "^8.0.1",
"rehype": "^13.0.1",
Expand Down
4 changes: 2 additions & 2 deletions packages/vitepress-plugin-og-image/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { dirname, join, relative, resolve, sep } from 'node:path'
import { fileURLToPath } from 'node:url'
import type { Buffer } from 'node:buffer'
import fs from 'fs-extra'
import fg from 'fast-glob'
import { glob } from 'glob'
import type { DefaultTheme, SiteConfig } from 'vitepress'
import { gray, green, red, yellow } from 'colorette'
import GrayMatter from 'gray-matter'
Expand Down Expand Up @@ -234,7 +234,7 @@ export function buildEndGenerateOpenGraphImages(options: {
pages = pages.concat(items)
}

const files = await fg(`${siteConfig.outDir}/**/*.html`, { onlyFiles: true })
const files = await glob(`${siteConfig.outDir}/**/*.html`, { nodir: true })

if (!ogImageTemplateSvgPath) {
return ''
Expand Down
Loading

0 comments on commit 087ae0f

Please sign in to comment.