Repository navigation
Find local images beside the Markdown file - #96
Open
MattFisher wants to merge 1 commit into
Open
MattFisher wants to merge 1 commit into
MattFisher wants to merge 1 commit into
Conversation
Relative image paths in Markdown only resolved from the working directory, so `substack drafts create posts/post.md` could not find `` 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
substack drafts create posts/post.mdcannot find an image stored next to the post unless you run it from insideposts/. 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:
running
substack drafts create posts/post.mdfrom the project root fails withLocal 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_markdownandApi.update_draft_from_markdowngain an optionalmarkdown_dirargument. It defaults toNone, which is today's behaviour.substack drafts create,substack drafts update,substack-publish-markdownandsubstack-publish-yamlpass 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
FileNotFoundErrornow 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
~paths, a directory with the image's name, remote URLs, bothApimethods, and all four commands.pytest -q -m "not live" --strict-markers: 310 passed.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()inmdrender.py, so there may be a small rebase depending on merge order. I'm happy to do it.🤖 Generated with Claude Code