# Link rewriting

# Link rewriting

# Link rewriting

Rewrites relative Markdown links between repo files to the target page's
absolute Storefront path, ahead of the push. Browser-relative
resolution of the *rendered* page is unreliable, so this
resolves against the source file's position in the repo instead, which is
correct independently of how Storefront later renders the page.


## LinkWarning

```rust
pub struct LinkWarning { pub file : String, pub line : usize, pub message : String, pub broken : bool, }
```

An unresolved or deferred link, named by the file and 1-based line it was
found on. `broken` is true only for a `*.md` link that could not be
resolved; `--strict` treats those, and only those, as a failure.


## MediaReference

```rust
pub struct MediaReference { pub file : String, pub line : usize, pub dest_path : String, pub resolved_path : Option < String >, }
```

A link/image destination pointing at a local non-Markdown file (a media
upload), named by the Markdown file and 1-based line it was found
on. `resolved_path` is the repo-relative path the destination resolves
to, `None` if it climbs above the repo root.


## rewrite_links

```rust
pub fn rewrite_links(file_path : & str, source : & str, page_paths : & HashMap < String, String >, media_urls : & HashMap < String, String >,) ->(String, Vec < LinkWarning >, Vec < MediaReference >);
```

Rewrites every relative Markdown link/image in `source` (the content of
`file_path`, a repo-relative path) whose target is a `*.md` file present
in `page_paths` (external id, i.e. repo-relative file path -> absolute
Storefront page path) to that absolute path, and every one whose target
is a local non-Markdown file present in `media_urls` (repo-relative path
-> uploaded media URL) to that URL. A non-Markdown local file not
in `media_urls` is left untouched and reported as a `MediaReference`
rather than rewritten, so the caller can upload it and try again. Links
that cannot be resolved are left untouched and reported as warnings.
Links in code blocks/spans and external URLs are untouched.
