FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add resolveCwd option and change default resolution behavior by mshick · Pull Request #270 · contentlayerdev/contentlayer · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .json  (1) .lock  (1) .mdx  (2) .png  (2) .ts  (3) All 5 file types selected
Only manifest files
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
        • mdx.ts
      • plugin.ts
      • package.json
          • image-a.png
          • post-a.mdx
          • image-b.png
          • post-b.mdx
        • index.test.ts
  • yarn.lock
12 changes: 9 additions & 3 deletions packages/@contentlayer/core/src/markdown/mdx.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,18 @@ export const bundleMDX = ({
if (mdxString.length === 0) {
return ''
}
const { rehypePlugins, remarkPlugins, cwd: cwd_, ...restOptions } = options ?? {}
const { rehypePlugins, remarkPlugins, resolveCwd, cwd: cwd_, ...restOptions } = options ?? {}

const getCwdFromContentDirPath = () =>
// TODO don't use `process.cwd()` but instead `HasCwd`
path.isAbsolute(contentDirPath) ? contentDirPath : path.join(process.cwd(), contentDirPath)
const cwd = cwd_ ?? getCwdFromContentDirPath()

const getRelativeCwd = () =>
path.join(getCwdFromContentDirPath(), path.dirname(rawDocumentData.flattenedPath))

const getCwd = () =>
resolveCwd === 'contentDirPath' ? getCwdFromContentDirPath() : getRelativeCwd()

const mdxOptions: BundleMDXOptions<any> = {
mdxOptions: (opts) => {
opts.rehypePlugins = [...(opts.rehypePlugins ?? []), ...(rehypePlugins ?? [])]
Expand All @@ -42,7 +47,8 @@ export const bundleMDX = ({
]
return opts
},
cwd,
// User-provided cwd trumps resolution
cwd: cwd_ ?? getCwd(),
// NOTE `restOptions` should be spread at the end to allow for user overrides
...restOptions,
}
Expand Down
10 changes: 9 additions & 1 deletion packages/@contentlayer/core/src/plugin.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,15 @@ export type MDXOptions = {
*
* If you're providing `mdxOptions` then `rehypePlugins` and `remarkPlugins` will be ignored.
*/
mdxOptions?: MDXBundlerMDXOptions
mdxOptions?: MDXBundlerMDXOptions,
/**
* How we resolve the cwd passed to mdx-bundler when processing a file. If an explicit `cwd`
* is provided this option will be ignored.
* - `relative` sets the cwd to the directory the file resides in.
* - `contentDirPath` sets the cwd to the contentDirPath. This was the default behavior up until v0.2.6.
* @default "relative"
*/
resolveCwd?: 'relative' | 'contentDirPath';
} & Omit<mdxBundler.BundleMDXOptions<any>, 'mdxOptions'>

export type MDXBundlerMDXOptions = mdxBundler.BundleMDXOptions<any>['mdxOptions']
Expand Down
1 change: 1 addition & 0 deletions packages/integration-tests/package.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
},
"devDependencies": {
"contentlayer": "workspace:*",
"remark-mdx-images": "^2.0.0",
"typescript": "^4.7.4",
"vite": "^2.9.8",
"vitest": "^0.12.4"
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Viewer requires iframe.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Hello world

![test](./image-a.png)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Viewer requires iframe.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Hello world

![test](./posts/image-b.png)
101 changes: 101 additions & 0 deletions packages/integration-tests/src/mdx/index.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/* eslint-disable import/no-extraneous-dependencies */
import * as core from 'contentlayer/core'
import { defineDocumentType, makeSource } from 'contentlayer/source-files'
import * as fs from 'node:fs/promises'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import remarkMdxImages from 'remark-mdx-images'
import { expect, test } from 'vitest'

test('mdx - resolveCwd - contentDirPath', async () => {
const Post = defineDocumentType(() => ({
name: 'Post',
filePathPattern: 'posts/*.mdx',
contentType: 'mdx',
fields: {},
}))

const testDirPath = fileURLToPath(new URL('.', import.meta.url))
const testOutPath = `${testDirPath}/out`

await fs.rm(path.join(testDirPath, '.contentlayer'), { recursive: true, force: true })

process.env['INIT_CWD'] = testDirPath

const source = await makeSource({
contentDirPath: path.join(testDirPath, 'contentDirPath'),
documentTypes: [Post],
mdx: {
resolveCwd: 'contentDirPath',
remarkPlugins: [remarkMdxImages],
esbuildOptions: (options) => {
options.platform = 'node'
options.outdir = testOutPath
options.assetNames = `images/[name]-[hash]`
options.loader = {
...options.loader,
'.png': 'file'
}
options.publicPath = '/'
options.write = true
return options
}
}
})

await core.runMain({ tracingServiceName: 'test', verbose: false })(
core.generateDotpkg({ config: { source, esbuildHash: 'STATIC_HASH' }, verbose: true }),
)

// Check that the bundled image has been generated
const statResult = await fs.stat(path.join(testOutPath, 'images/image-b-QQWYPTMT.png')).catch(() => false)

expect(statResult).not.toEqual(false);
})


test('mdx - resolveCwd - relative', async () => {
const Post = defineDocumentType(() => ({
name: 'Post',
filePathPattern: 'posts/*.mdx',
contentType: 'mdx',
fields: {},
}))

const testDirPath = fileURLToPath(new URL('.', import.meta.url))
const testOutPath = `${testDirPath}/out`

await fs.rm(path.join(testDirPath, '.contentlayer'), { recursive: true, force: true })

process.env['INIT_CWD'] = testDirPath

const source = await makeSource({
contentDirPath: path.join(testDirPath, 'content'),
documentTypes: [Post],
mdx: {
resolveCwd: 'relative',
remarkPlugins: [remarkMdxImages],
esbuildOptions: (options) => {
options.platform = 'node'
options.outdir = testOutPath
options.assetNames = `images/[name]-[hash]`
options.loader = {
...options.loader,
'.png': 'file'
}
options.publicPath = '/'
options.write = true
return options
}
}
})

await core.runMain({ tracingServiceName: 'test', verbose: false })(
core.generateDotpkg({ config: { source, esbuildHash: 'STATIC_HASH' }, verbose: true }),
)

// Check that the bundled image has been generated
const statResult = await fs.stat(path.join(testOutPath, 'images/image-a-QQWYPTMT.png')).catch(() => false)

expect(statResult).not.toEqual(false);
})
Loading

Back | FazBrowse Home | New Git URL