Function tpnote_lib::html::rewrite_links

source ·
pub fn rewrite_links(
    html: String,
    root_path: &Path,
    docdir: &Path,
    rewrite_rel_links: bool,
    rewrite_abs_links: bool,
    rewrite_ext: bool,
    allowed_local_links: Arc<RwLock<HashSet<PathBuf>>>
) -> String
Expand description

Helper function that scans the input html string and converts all relative local HTML links to absolute links.

The base path for this conversion (usually where the HTML file resides), is docdir. If not rewrite_rel_links, relative local links are not converted. Furthermore, all local absolute (not converted) links are prepended with root_path. All external URLs always remain untouched. If rewrite_abs_links and link is absolute, concatenate and return root_path and dest. If rewrite_ext is true and the link points to a known Tp-Note file extension, then .html is appended to the converted link. Remark: The anchor’s text property is never changed. However, there is one exception: when the text contains a URL starting with http: or https:, only the file stem is kept. Example, the anchor text property: <a ...>http:dir/my file.md</a> is rewritten into <a ...>my file</a>.

It is guaranteed, that all local links in the converted html point inside root_path. If not, the link is displayed as INVALID LOCAL LINK and discarded. All valid local links are inserted in allowed_local_links the same way as their destination appears in the resulting HTML.