Skip to content

Commit

Permalink
fix: replace referenced markdown (#4191)
Browse files Browse the repository at this point in the history
* fix: replace referenced markdown

* add dry test

* fix
  • Loading branch information
abvthecity authored Aug 2, 2024
1 parent fe4da35 commit 268a463
Show file tree
Hide file tree
Showing 8 changed files with 228 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/cli/docs-resolver/src/DocsDefinitionResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ export class DocsDefinitionResolver {
absolutePathToMdx: this.resolveFilepath(relativePath),
context: this.taskContext
});
}

// replaces all instances of <Code src="path/to/file.js" /> with the content of the referenced code file
for (const [relativePath, markdown] of Object.entries(this.parsedDocsConfig.pages)) {
this.parsedDocsConfig.pages[RelativeFilePath.of(relativePath)] = await replaceReferencedCode({
markdown,
absolutePathToFernFolder: this.docsWorkspace.absoluteFilepath,
Expand Down
138 changes: 138 additions & 0 deletions packages/cli/docs-resolver/src/__test__/__snapshots__/dry.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`converts to api reference node 1`] = `
"# Relative Path
### Snippet
Hello, I'm a snippet.
\`\`\`js title="fibonacci.js"
function fibonacci(num) {
let n1 = 0, n2 = 1, nextTerm;
console.log('Fibonacci Sequence:');
for (let i = 1; i <= num; i++) {
console.log(n1);
nextTerm = n1 + n2;
n1 = n2;
n2 = nextTerm;
}
}
// Example: Log the first 10 terms of the Fibonacci sequence
fibonacci(10);
\`\`\`
# Relative Path 2
### Snippet
Hello, I'm a snippet.
\`\`\`js title="fibonacci.js"
function fibonacci(num) {
let n1 = 0, n2 = 1, nextTerm;
console.log('Fibonacci Sequence:');
for (let i = 1; i <= num; i++) {
console.log(n1);
nextTerm = n1 + n2;
n1 = n2;
n2 = nextTerm;
}
}
// Example: Log the first 10 terms of the Fibonacci sequence
fibonacci(10);
\`\`\`
# Relative Path 3
### Snippet
Hello, I'm a snippet.
\`\`\`js title="fibonacci.js"
function fibonacci(num) {
let n1 = 0, n2 = 1, nextTerm;
console.log('Fibonacci Sequence:');
for (let i = 1; i <= num; i++) {
console.log(n1);
nextTerm = n1 + n2;
n1 = n2;
n2 = nextTerm;
}
}
// Example: Log the first 10 terms of the Fibonacci sequence
fibonacci(10);
\`\`\`
# Absolute Path
### Snippet
Hello, I'm a snippet.
\`\`\`js title="fibonacci.js"
function fibonacci(num) {
let n1 = 0, n2 = 1, nextTerm;
console.log('Fibonacci Sequence:');
for (let i = 1; i <= num; i++) {
console.log(n1);
nextTerm = n1 + n2;
n1 = n2;
n2 = nextTerm;
}
}
// Example: Log the first 10 terms of the Fibonacci sequence
fibonacci(10);
\`\`\`
# Indented
<div>
### Snippet
Hello, I'm a snippet.
\`\`\`js title="fibonacci.js"
function fibonacci(num) {
let n1 = 0, n2 = 1, nextTerm;
console.log('Fibonacci Sequence:');
for (let i = 1; i <= num; i++) {
console.log(n1);
nextTerm = n1 + n2;
n1 = n2;
n2 = nextTerm;
}
}
// Example: Log the first 10 terms of the Fibonacci sequence
fibonacci(10);
\`\`\`
</div>
"
`;
32 changes: 32 additions & 0 deletions packages/cli/docs-resolver/src/__test__/dry.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { docsYml } from "@fern-api/configuration";
import { AbsoluteFilePath, resolve } from "@fern-api/fs-utils";
import { createMockTaskContext } from "@fern-api/task-context";
import { loadDocsWorkspace } from "@fern-api/workspace-loader";
import { DocsDefinitionResolver } from "../DocsDefinitionResolver";

const context = createMockTaskContext();

it("converts to api reference node", async () => {
const docsWorkspace = await loadDocsWorkspace({
fernDirectory: resolve(AbsoluteFilePath.of(__dirname), "fixtures/dry/fern"),
context
});

if (docsWorkspace == null) {
throw new Error("Workspace is null");
}

const resolver = new DocsDefinitionResolver(
"domain",
docsWorkspace,
[],
context,
undefined,
async (_files) => [],
async (_opts) => ""
);

const resolved = await resolver.resolve();

expect(resolved.pages["page.mdx"]?.markdown).toMatchSnapshot();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
instances: []
navigation:
- page: Testing Page
path: ./page.mdx
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function fibonacci(num) {
let n1 = 0, n2 = 1, nextTerm;

console.log('Fibonacci Sequence:');
for (let i = 1; i <= num; i++) {
console.log(n1);
nextTerm = n1 + n2;
n1 = n2;
n2 = nextTerm;
}
}

// Example: Log the first 10 terms of the Fibonacci sequence
fibonacci(10);
33 changes: 33 additions & 0 deletions packages/cli/docs-resolver/src/__test__/fixtures/dry/fern/page.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Relative Path

<Markdown src="./snippet.md" />

<Code src="./fibonacci.js" />

# Relative Path 2

<Markdown src="snippet.md" />

<Code src="fibonacci.js" />

# Relative Path 3

<Markdown src="../fern/snippet.md" />

<Code src="../fern/fibonacci.js" />

# Absolute Path

<Markdown src="/snippet.md" />

<Code src="/fibonacci.js" />

# Indented

<div>

<Markdown src="./snippet.md" />

<Code src="./fibonacci.js" />

</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Snippet

Hello, I'm a snippet.

0 comments on commit 268a463

Please sign in to comment.