Skip to content

Find local images beside the Markdown file - #96

Open
MattFisher wants to merge 1 commit into
ma2za:mainfrom
MattFisher:fix/markdown-relative-image-paths
Open

MattFisher wants to merge 1 commit into
ma2za:mainfrom
MattFisher:fix/markdown-relative-image-paths

Conversation

@MattFisher

Copy link
Copy Markdown
Contributor

substack drafts create posts/post.md cannot find an image stored next to the post unless you run it from inside posts/. This PR makes the draft commands also look in the Markdown file's folder.

The problem

Relative image paths resolve from the working directory only. With this layout:

posts/post.md            contains ![chart](figures/chart.png)
posts/figures/chart.png

running substack drafts create posts/post.md from the project root fails with Local image file not found: figures/chart.png. The path is correct relative to the post, and it is what editors and Markdown previewers expect.

The change

  • mdrender.markdown_to_doc, Post.from_markdown, Api.create_draft_from_markdown and Api.update_draft_from_markdown gain an optional markdown_dir argument. It defaults to None, which is today's behaviour.
  • When it is set, a relative image path that is not found in the working directory is looked up in that folder.
  • substack drafts create, substack drafts update, substack-publish-markdown and substack-publish-yaml pass the Markdown file's folder. For inline Markdown in a YAML file, they pass the YAML file's folder.

Compatibility

The working directory is still checked first, so every path that resolves today resolves to the same file. The only difference is that some paths that used to raise FileNotFoundError now resolve. Absolute paths, ~ paths, the legacy leading-/ fallback, remote URLs and the MCP server (which has no file) are unchanged.

Checking the Markdown file's folder first would match other Markdown tools more closely, but it would change which file is uploaded when both exist, so I kept the working directory first. I'm happy to switch the order if you prefer it.

Testing

  • New offline tests cover: images found beside the Markdown file (top level, block quote, list, callout, footnote), the working directory taking priority, a file missing from both places, rooted and ~ paths, a directory with the image's name, remote URLs, both Api methods, and all four commands.
  • pytest -q -m "not live" --strict-markers: 310 passed.
  • Docs (docs/markdown.md, docs/cli.md, docs/yaml.md, docs/troubleshooting.md) and the changelog are updated.

Note

#94 and #95 also touch _captioned_image() in mdrender.py, so there may be a small rebase depending on merge order. I'm happy to do it.

🤖 Generated with Claude Code

Relative image paths in Markdown only resolved from the working
directory, so `substack drafts create posts/post.md` could not find
`![chart](figures/chart.png)` stored next to the post unless it was run
from inside `posts/`.

Add an optional `markdown_dir` argument to `markdown_to_doc`,
`Post.from_markdown`, `Api.create_draft_from_markdown` and
`Api.update_draft_from_markdown`. A relative image path that is not
found in the working directory is then looked up in that folder. The
draft commands pass the Markdown file's folder.

The working directory is still checked first, so every path that
resolves today resolves to the same file.

Co-Authored-By: Claude <noreply@anthropic.com>

This branch has not been deployed

No deployments
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

Successfully merging this pull request may close these issues.

1 participant