Skip to content

fix: image src not rewritten in markdown when page title contains an apostrophe - #16

Merged
takeoverjp merged 2 commits into
mainfrom
15-bug-image-src-not-rewritten-in-markdown-when-page-title-contains-an-apostrophe
Jul 2, 2026
Merged

fix: image src not rewritten in markdown when page title contains an apostrophe#16
takeoverjp merged 2 commits into
mainfrom
15-bug-image-src-not-rewritten-in-markdown-when-page-title-contains-an-apostrophe

Conversation

@takeoverjp

Copy link
Copy Markdown
Contributor

…he (#15)

## Problem

When a Confluence page title contained an apostrophe (`'`), draw.io and
regular image assets were not recognized as local files and the tool
attempted to re-download them as remote URLs, resulting in a warning and
a failed fetch:

  WARN Failed to fetch image: https://example.com/confluence2md's_…_assets%2Fsingle.drawio.png

## Root cause

`to_markdown_asset_path` (utils.rs) encodes asset paths using the
`URI_COMPONENT` character set, which leaves `'` as a literal character.
`is_local_markdown_asset` (confluence.rs) built its comparison prefix
using the `PATH_SEGMENT` character set, which encodes `'` as `%27`.
The resulting mismatch caused the prefix check to fail for any page
title containing an apostrophe, so every asset on such a page was
treated as a remote URL.

  // old: PATH_SEGMENT encodes ' → %27
  encoded_prefix = "confluence2md%27s_…_assets"

  // src produced by to_markdown_asset_path (URI_COMPONENT, ' is literal)
  src = "confluence2md's_…_assets%2Fsingle.drawio.png"

  // starts_with check → false → asset downloaded as remote URL

## Fix

Change `is_local_markdown_asset` to encode the comparison prefix with
`URI_COMPONENT` instead of `PATH_SEGMENT`, matching the encoding used by
`to_markdown_asset_path`. Export `URI_COMPONENT` as `pub` from utils.rs
and import it in confluence.rs.

## Tests

Added regression tests in `confluence::tests`:
- `is_local_markdown_asset_recognizes_apostrophe_in_prefix` — verifies
  that a path generated by `to_markdown_asset_path` with an apostrophe
  in the prefix is recognized as a local asset (would fail with the old
  `PATH_SEGMENT`-based code)
- `is_local_markdown_asset_recognizes_plain_prefix` — ensures the
  ordinary case continues to work
- `is_local_markdown_asset_rejects_remote_url` — ensures remote URLs
  are not mistakenly treated as local assets
When a Confluence page title contains an apostrophe (e.g. "My team's page"),
the Confluence REST API renders image `src` attributes using single-quoted
HTML attribute values (e.g. `src='…/My%20team%27s%20page/image.png'`).

The previous regex used a back-reference approach (`["']` open + `["']`
close) that verified the opening and closing quotes matched.  However, if
the captured src string itself contained a single-quote character the regex
engine would stop the lazy `.*?` match early—at the embedded quote—causing
the closing-quote group to see a mismatch and skip the rewrite entirely.
As a result the local asset path was never substituted and the generated
Markdown still pointed at the remote Confluence URL.

Fix: rewrite the regex to use two independent alternating groups
(`"([^"]*)"` and `'([^']*)'`) so each branch only matches content that
cannot contain its own delimiter.  The replacement logic picks the correct
quote character from whichever group matched, making the rewrite robust
regardless of which delimiter Confluence chose for the attribute.

Also adds a regression test that mocks a Confluence image endpoint whose
path contains a percent-encoded apostrophe and asserts the resulting HTML
has the src rewritten to a local asset path.
@takeoverjp takeoverjp changed the title 15 bug image src not rewritten in markdown when page title contains an apostrophe fix: image src not rewritten in markdown when page title contains an apostrophe Jul 2, 2026
@takeoverjp
takeoverjp merged commit f0e4cb0 into main Jul 2, 2026
1 check passed
@takeoverjp
takeoverjp deleted the 15-bug-image-src-not-rewritten-in-markdown-when-page-title-contains-an-apostrophe branch July 2, 2026 15:55
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.

Bug: image src not rewritten in Markdown when page title contains an apostrophe

1 participant