histoire-dev / histoire

⚡ Fast and beautiful interactive component playgrounds, powered by Vite
https://histoire.dev
MIT License
3.19k stars 190 forks source link

Links in markdown files depend on the resolve order #562

Open ch1ller0 opened 1 year ago

ch1ller0 commented 1 year ago

Describe the bug

Good evening! I have noticed that histoire build will fail if you have a link which references a file which is not in the ctx yet. Consider these two files: src/file1.story.md

---
title: Docs/File1
id: file1
---

- [Link to file 2](./forContributors/file2.story.md)

src/file2.story.md

---
title: Docs/File2
id: file2
---

- [Link to file 1](../file1.story.md)

If I run histoire dev (or build - it doesn't matter), I get the following error:

Error: [md] Cannot find story file: ./file2.story.md from /home/projects/histoire-vue3-starter-jpcz1f/src/file1.story.md
    at md.renderer.rules.link_open (file:///home/projects/histoire-vue3-starter-jpcz1f/node_modules/histoire/dist/node/markdown.js:79:31)
    at Renderer.renderInline (file:///home/projects/histoire-vue3-starter-jpcz1f/node_modules/markdown-it/lib/renderer.js:275:28)
    at Renderer.render (file:///home/projects/histoire-vue3-starter-jpcz1f/node_modules/markdown-it/lib/renderer.js:330:22)
    at MarkdownIt.render (file:///home/projects/histoire-vue3-starter-jpcz1f/node_modules/markdown-it/lib/index.js:544:24)
    at addFile (file:///home/projects/histoire-vue3-starter-jpcz1f/node_modules/histoire/dist/node/markdown.js:147:25)
    at async FSWatcher.eval (file:///home/projects/histoire-vue3-starter-jpcz1f/node_modules/histoire/dist/node/markdown.js:203:9)

After a small research I have found that the problem was that markdown-it for file1 starts running link_open rule before actually finishing collecting all the .md files from the .md watcher. For simplicity I have attached a repro in Stackblitz

Reproduction

https://stackblitz.com/edit/histoire-vue3-starter-jpcz1f?file=src%2Ffile2.story.md,src%2Ffile1.story.md

System Info

System:
    OS: macOS 13.3.1
    CPU: (10) arm64 Apple M1 Pro
    Memory: 64.95 MB / 16.00 GB
    Shell: 3.5.1 - /opt/homebrew/bin/fish/
  Binaries:
    Node: 16.20.1 - /opt/homebrew/opt/node@16/bin/node
    Yarn: 1.22.19 - /opt/homebrew/bin/yarn
    npm: 8.19.4 - /opt/homebrew/opt/node@16/bin/npm
    pnpm: 8.6.6 - /opt/homebrew/bin/pnpm
  Browsers:
    Chrome: 115.0.5790.102
    Safari: 16.4

Used Package Manager

npm

Validations

stackblitz[bot] commented 1 year ago

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.