Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: replace referenced markdown #4191

Merged
merged 3 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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);
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.
Loading