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

import code from external file is not work #488

Open
micooz opened this issue Dec 8, 2024 · 0 comments
Open

import code from external file is not work #488

micooz opened this issue Dec 8, 2024 · 0 comments

Comments

@micooz
Copy link

micooz commented Dec 8, 2024

The docs shows Code Hike supports import external code, but it actually not work.

https://codehike.org/docs/concepts/code#importing-code-from-a-file-into-a-codeblock

Reproduce

First create a new project,

npx create-next-app -e https://github.com/code-hike/v1-starter

Then an external file app/external.js,

console.log("Hello, world!")

Then import it from app/page.mdx, use !from syntax:

    ```js
    !from "./external.js"
    ```

You will see an error:

./app/page.mdx
Error: page.mdx:Error: Code Hike couldn't resolve this annotation:
!from /xxx/app/page.mdx 
/xxx/app/"./external.js" doesn't exist.
    at async Promise.all (index 0)

This is because file.history is empty (line 27,32,48), but I don't know why.

const mdxPath = file?.history ? file.history[file.history.length - 1] : null
await Promise.all(
nodes.map(async (code) => {
const fromData = code.value.slice(6).trim()
const [codepath, range] = fromData?.split(/\s+/) || []
code.value = await readFile(codepath, mdxPath, range)
}),
)
return tree
}
async function readFile(
externalCodePath: string,
mdxFilePath: string | null,
range: string | undefined,
) {
const annotationContent = "!from " + mdxFilePath + " " + (range || "")
// if we don't know the path of the mdx file:
if (mdxFilePath == null) {
throw new Error(
`Code Hike couldn't resolve this annotation:
${annotationContent}
Someone is calling the mdx compile function without setting the path.
Open an issue on CodeHike's repo for help.`,
)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant