11import fs from 'fs' ;
22import path from 'path' ;
3+ import { fileURLToPath } from 'url' ;
34
45import matter from 'gray-matter' ;
56import { s } from 'hastscript' ;
@@ -28,7 +29,8 @@ import remarkTocHeadings, {TocNode} from './remark-toc-headings';
2829import remarkVariables from './remark-variables' ;
2930import { FrontMatter , Platform , PlatformConfig } from './types' ;
3031
31- const root = process . cwd ( ) ;
32+ // @ts -ignore
33+ const directoryName = path . dirname ( fileURLToPath ( import . meta. url ) ) ;
3234
3335function formatSlug ( slug : string ) {
3436 return slug . replace ( / \. ( m d x | m d ) / , '' ) ;
@@ -96,8 +98,8 @@ async function getDocsFrontMatterUncached(): Promise<FrontMatter[]> {
9698 return frontMatter ;
9799}
98100
99- export function getAllFilesFrontMatter ( folder : string = 'docs' ) {
100- const docsPath = path . join ( root , folder ) ;
101+ function getAllFilesFrontMatter ( ) {
102+ const docsPath = path . join ( directoryName , '..' , 'docs' ) ;
101103 const files = getAllFilesRecursively ( docsPath ) ;
102104 const allFrontMatter : FrontMatter [ ] = [ ] ;
103105 files . forEach ( file => {
@@ -115,15 +117,10 @@ export function getAllFilesFrontMatter(folder: string = 'docs') {
115117 allFrontMatter . push ( {
116118 ...( frontmatter as FrontMatter ) ,
117119 slug : formatSlug ( fileName ) ,
118- sourcePath : path . join ( folder , fileName ) ,
120+ sourcePath : path . join ( 'docs' , fileName ) ,
119121 } ) ;
120122 } ) ;
121123
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-
127124 // Add all `common` files in the right place.
128125 const platformsPath = path . join ( docsPath , 'platforms' ) ;
129126 const platformNames = fs
@@ -218,17 +215,17 @@ export function getAllFilesFrontMatter(folder: string = 'docs') {
218215}
219216
220217export async function getFileBySlug ( slug : string ) {
221- const configPath = path . join ( root , slug , 'config.yml' ) ;
218+ const configPath = path . join ( directoryName , '..' , slug , 'config.yml' ) ;
222219
223220 let configFrontmatter : PlatformConfig | undefined ;
224221 if ( fs . existsSync ( configPath ) ) {
225222 configFrontmatter = yaml . load ( fs . readFileSync ( configPath , 'utf8' ) ) as PlatformConfig ;
226223 }
227224
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' ) ;
232229
233230 if (
234231 slug . indexOf ( 'docs/platforms/' ) === 0 &&
@@ -249,18 +246,20 @@ export async function getFileBySlug(slug: string) {
249246 commonFilePath = path . join ( commonPath , slugParts . slice ( 3 ) . join ( '/' ) ) ;
250247 }
251248 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' ) ;
256253 }
257254 }
258255
259- const sourcePath = [ mdxPath , mdxIndexPath , mdPath ] . find ( fs . existsSync ) ?? mdIndexPath ;
256+ const sourcePath =
257+ [ mdxPath , mdxIndexPath , mdPath ] . find ( p => fs . existsSync ( p ) ) ?? mdIndexPath ;
260258 const source = fs . readFileSync ( sourcePath , 'utf8' ) ;
261259
262260 process . env . ESBUILD_BINARY_PATH = path . join (
263- root ,
261+ directoryName ,
262+ '..' ,
264263 'node_modules' ,
265264 'esbuild' ,
266265 'bin' ,
@@ -285,7 +284,10 @@ export async function getFileBySlug(slug: string) {
285284 [ remarkTocHeadings , { exportRef : toc } ] ,
286285 remarkGfm ,
287286 remarkFormatCodeBlocks ,
288- [ remarkImageSize , { sourceFolder : cwd , publicFolder : path . join ( root , 'public' ) } ] ,
287+ [
288+ remarkImageSize ,
289+ { sourceFolder : cwd , publicFolder : path . join ( directoryName , '..' , 'public' ) } ,
290+ ] ,
289291 remarkMdxImages ,
290292 remarkCodeTitles ,
291293 remarkCodeTabs ,
@@ -353,7 +355,7 @@ export async function getFileBySlug(slug: string) {
353355 } ;
354356 // Set the `outdir` to a public location for this bundle.
355357 // 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' ) ;
357359
358360 // Set write to true so that esbuild will output the files.
359361 options . write = true ;
0 commit comments