Function tpnote_lib::html::rewrite_links

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

A helper function that scans the input HTML document in html_input for HTML hyperlinks. When it finds a relative URL (local link), it analyzes it’s path. A relative path is then converted into an absolute path, before the result is reinserted into the HTML document.

The base path for this conversion is docdir, the location of the HTML document. If not rewrite_rel_paths, relative local paths are not converted. Furthermore, all local absolute (not converted) paths are prepended with root_path. All external URLs always remain untouched. If rewrite_abs_paths and the URL’s path is absolute, it prepends root_path. Finally, if rewrite_ext is true and a local link points to a known Tp-Note file extension, then .html is appended to the converted link. Remark: The link’s text property is never changed. However, there is one exception: when the link’s text contains a string similar to URLs, starting with http: or tpnote:. In this case, the string is interpreted URL and only the stem of the filename is displayed, e.g. <a ...>http:dir/my file.md</a> is rewritten into <a ...>my file</a>.

Before a local (converted) link is reinserted in the output HTML, a copy is inserted into allowed_local_links for further bookkeeping.

decoded. After rewriting, the links are finally HTML escape encoded before the are reinserted in the output HTML of this function. NB2: It is guaranteed, that the resulting HTML document contains only local links to other documents within root_path. Deviant links displayed as INVALID LOCAL LINK and URL is discarded.