1
1
import fs from 'fs' ;
2
2
import path from 'path' ;
3
+ import { fileURLToPath } from 'url' ;
3
4
4
5
import matter from 'gray-matter' ;
5
6
import { s } from 'hastscript' ;
@@ -28,7 +29,8 @@ import remarkTocHeadings, {TocNode} from './remark-toc-headings';
28
29
import remarkVariables from './remark-variables' ;
29
30
import { FrontMatter , Platform , PlatformConfig } from './types' ;
30
31
31
- const root = process . cwd ( ) ;
32
+ // @ts -ignore
33
+ const directoryName = path . dirname ( fileURLToPath ( import . meta. url ) ) ;
32
34
33
35
function formatSlug ( slug : string ) {
34
36
return slug . replace ( / \. ( m d x | m d ) / , '' ) ;
@@ -96,8 +98,8 @@ async function getDocsFrontMatterUncached(): Promise<FrontMatter[]> {
96
98
return frontMatter ;
97
99
}
98
100
99
- export function getAllFilesFrontMatter ( folder : string = 'docs' ) {
100
- const docsPath = path . join ( root , folder ) ;
101
+ function getAllFilesFrontMatter ( ) {
102
+ const docsPath = path . join ( directoryName , '..' , 'docs' ) ;
101
103
const files = getAllFilesRecursively ( docsPath ) ;
102
104
const allFrontMatter : FrontMatter [ ] = [ ] ;
103
105
files . forEach ( file => {
@@ -115,15 +117,10 @@ export function getAllFilesFrontMatter(folder: string = 'docs') {
115
117
allFrontMatter . push ( {
116
118
...( frontmatter as FrontMatter ) ,
117
119
slug : formatSlug ( fileName ) ,
118
- sourcePath : path . join ( folder , fileName ) ,
120
+ sourcePath : path . join ( 'docs' , fileName ) ,
119
121
} ) ;
120
122
} ) ;
121
123
122
- if ( folder !== 'docs' ) {
123
- // We exit early if we're not in the docs folder. We use this for the changelog.
124
- return allFrontMatter ;
125
- }
126
-
127
124
// Add all `common` files in the right place.
128
125
const platformsPath = path . join ( docsPath , 'platforms' ) ;
129
126
const platformNames = fs
@@ -218,17 +215,17 @@ export function getAllFilesFrontMatter(folder: string = 'docs') {
218
215
}
219
216
220
217
export async function getFileBySlug ( slug : string ) {
221
- const configPath = path . join ( root , slug , 'config.yml' ) ;
218
+ const configPath = path . join ( directoryName , '..' , slug , 'config.yml' ) ;
222
219
223
220
let configFrontmatter : PlatformConfig | undefined ;
224
221
if ( fs . existsSync ( configPath ) ) {
225
222
configFrontmatter = yaml . load ( fs . readFileSync ( configPath , 'utf8' ) ) as PlatformConfig ;
226
223
}
227
224
228
- let mdxPath = path . join ( root , `${ slug } .mdx` ) ;
229
- let mdxIndexPath = path . join ( root , slug , 'index.mdx' ) ;
230
- let mdPath = path . join ( root , `${ slug } .md` ) ;
231
- let mdIndexPath = path . join ( root , slug , 'index.md' ) ;
225
+ let mdxPath = path . join ( directoryName , '..' , `${ slug } .mdx` ) ;
226
+ let mdxIndexPath = path . join ( directoryName , '..' , slug , 'index.mdx' ) ;
227
+ let mdPath = path . join ( directoryName , '..' , `${ slug } .md` ) ;
228
+ let mdIndexPath = path . join ( directoryName , '..' , slug , 'index.md' ) ;
232
229
233
230
if (
234
231
slug . indexOf ( 'docs/platforms/' ) === 0 &&
@@ -249,18 +246,20 @@ export async function getFileBySlug(slug: string) {
249
246
commonFilePath = path . join ( commonPath , slugParts . slice ( 3 ) . join ( '/' ) ) ;
250
247
}
251
248
if ( commonFilePath && fs . existsSync ( commonPath ) ) {
252
- mdxPath = path . join ( root , `${ commonFilePath } .mdx` ) ;
253
- mdxIndexPath = path . join ( root , commonFilePath , 'index.mdx' ) ;
254
- mdPath = path . join ( root , `${ commonFilePath } .md` ) ;
255
- mdIndexPath = path . join ( root , commonFilePath , 'index.md' ) ;
249
+ mdxPath = path . join ( directoryName , '..' , `${ commonFilePath } .mdx` ) ;
250
+ mdxIndexPath = path . join ( directoryName , '..' , commonFilePath , 'index.mdx' ) ;
251
+ mdPath = path . join ( directoryName , '..' , `${ commonFilePath } .md` ) ;
252
+ mdIndexPath = path . join ( directoryName , '..' , commonFilePath , 'index.md' ) ;
256
253
}
257
254
}
258
255
259
- const sourcePath = [ mdxPath , mdxIndexPath , mdPath ] . find ( fs . existsSync ) ?? mdIndexPath ;
256
+ const sourcePath =
257
+ [ mdxPath , mdxIndexPath , mdPath ] . find ( p => fs . existsSync ( p ) ) ?? mdIndexPath ;
260
258
const source = fs . readFileSync ( sourcePath , 'utf8' ) ;
261
259
262
260
process . env . ESBUILD_BINARY_PATH = path . join (
263
- root ,
261
+ directoryName ,
262
+ '..' ,
264
263
'node_modules' ,
265
264
'esbuild' ,
266
265
'bin' ,
@@ -285,7 +284,10 @@ export async function getFileBySlug(slug: string) {
285
284
[ remarkTocHeadings , { exportRef : toc } ] ,
286
285
remarkGfm ,
287
286
remarkFormatCodeBlocks ,
288
- [ remarkImageSize , { sourceFolder : cwd , publicFolder : path . join ( root , 'public' ) } ] ,
287
+ [
288
+ remarkImageSize ,
289
+ { sourceFolder : cwd , publicFolder : path . join ( directoryName , '..' , 'public' ) } ,
290
+ ] ,
289
291
remarkMdxImages ,
290
292
remarkCodeTitles ,
291
293
remarkCodeTabs ,
@@ -353,7 +355,7 @@ export async function getFileBySlug(slug: string) {
353
355
} ;
354
356
// Set the `outdir` to a public location for this bundle.
355
357
// this where this images will be copied
356
- options . outdir = path . join ( root , 'public' , 'mdx-images' ) ;
358
+ options . outdir = path . join ( directoryName , '..' , 'public' , 'mdx-images' ) ;
357
359
358
360
// Set write to true so that esbuild will output the files.
359
361
options . write = true ;
0 commit comments