1+ import { readFile } from "fs/promises" ;
12import { v4 as uuidv4 } from "uuid" ;
23
34import { DocsDefinitionResolver , filterOssWorkspaces } from "@fern-api/docs-resolver" ;
@@ -13,27 +14,63 @@ import {
1314 convertDbDocsConfigToRead ,
1415 convertDocsDefinitionToDb
1516} from "@fern-api/fdr-sdk" ;
16- import { convertToFernHostAbsoluteFilePath } from "@fern-api/fs-utils" ;
17+ import { AbsoluteFilePath , convertToFernHostAbsoluteFilePath , relative } from "@fern-api/fs-utils" ;
1718import { IntermediateRepresentation } from "@fern-api/ir-sdk" ;
1819import { Project } from "@fern-api/project-loader" ;
1920import { convertIrToFdrApi } from "@fern-api/register" ;
2021import { TaskContext } from "@fern-api/task-context" ;
2122
23+ import { replaceReferencedMarkdown } from "../../docs-markdown-utils/src" ;
24+
2225export async function getPreviewDocsDefinition ( {
2326 domain,
2427 project,
25- context
28+ context,
29+ previousDocsDefinition,
30+ editedAbsoluteFilepaths
2631} : {
2732 domain : string ;
2833 project : Project ;
2934 context : TaskContext ;
35+ previousDocsDefinition ?: DocsV1Read . DocsDefinition ;
36+ editedAbsoluteFilepaths ?: AbsoluteFilePath [ ] ;
3037} ) : Promise < DocsV1Read . DocsDefinition > {
3138 const docsWorkspace = project . docsWorkspaces ;
3239 const apiWorkspaces = project . apiWorkspaces ;
3340 if ( docsWorkspace == null ) {
3441 throw new Error ( "No docs workspace found in project" ) ;
3542 }
3643
44+ if ( editedAbsoluteFilepaths != null && previousDocsDefinition != null ) {
45+ const allMarkdownFiles = editedAbsoluteFilepaths . every (
46+ ( filepath ) => filepath . endsWith ( ".mdx" ) || filepath . endsWith ( ".md" )
47+ ) ;
48+ for ( const absoluteFilePath of editedAbsoluteFilepaths ) {
49+ const relativePath = relative ( docsWorkspace . absoluteFilePath , absoluteFilePath ) ;
50+ const markdown = ( await readFile ( absoluteFilePath ) ) . toString ( ) ;
51+ const processedMarkdown = await replaceReferencedMarkdown ( {
52+ markdown,
53+ absolutePathToFernFolder : docsWorkspace . absoluteFilePath ,
54+ absolutePathToMarkdownFile : absoluteFilePath ,
55+ context
56+ } ) ;
57+
58+ const previousValue = previousDocsDefinition . pages [ FdrAPI . PageId ( relativePath ) ] ;
59+ if ( previousValue == null ) {
60+ continue ;
61+ }
62+
63+ previousDocsDefinition . pages [ FdrAPI . PageId ( relativePath ) ] = {
64+ markdown : processedMarkdown ,
65+ editThisPageUrl : previousValue . editThisPageUrl
66+ } ;
67+ }
68+
69+ if ( allMarkdownFiles ) {
70+ return previousDocsDefinition ;
71+ }
72+ }
73+
3774 const fernWorkspaces = await Promise . all (
3875 apiWorkspaces . map (
3976 async ( workspace ) =>
0 commit comments