Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix anchor and subpath for relative links #22

Merged
merged 11 commits into from
Sep 11, 2024
13 changes: 11 additions & 2 deletions src/url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,18 @@ pub fn normalize_href(url: &str) -> Cow<str> {
warn!("Attempt to pass in dangerous URL: {url}");
Cow::Borrowed("#invalid-url")
} else {
let mut url = str!(url);
let split_anchor: Vec<&str> = url.splitn(2, "#").collect();
let mut split_url: Vec<&str> = split_anchor[0].split("/").collect();
if !split_url[0].is_empty() {
split_url.insert(0, "");
}
let mut url = str!(split_url[1]);
normalize(&mut url);
url.insert(0, '/');
split_url[1] = &url;
url = split_url.join("/");
if split_anchor.len() == 2 {
url = format!("{}#{}", url, split_anchor[1]);
}
Cow::Owned(url)
emmiegit marked this conversation as resolved.
Show resolved Hide resolved
}
}
Expand Down
1 change: 1 addition & 0 deletions test/link-subpath-anchor.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<wj-body class="wj-body"><p><a href="/relative/link#toc0" class="wj-link wj-link-internal" data-link-type="direct">text</a></p></wj-body>
46 changes: 46 additions & 0 deletions test/link-subpath-anchor.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"input": "[/relative/link#toc0 text]",
"tree": {
"elements": [
{
"element": "container",
"data": {
"type": "paragraph",
"attributes": {},
"elements": [
{
"element": "link",
"data": {
"type": "direct",
"link": "/relative/link#toc0",
"label": {
"text": "text"
},
"target": null
}
}
]
}
},
{
"element": "footnote-block",
"data": {
"title": null,
"hide": false
}
}
],
"html-blocks": [
],
"code-blocks": [
],
"table-of-contents": [
],
"footnotes": [
],
"bibliographies": [
]
},
"errors": [
]
}
1 change: 1 addition & 0 deletions test/link-subpath.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<wj-body class="wj-body"><p><a href="/relative/link" class="wj-link wj-link-internal" data-link-type="direct">text</a></p></wj-body>
46 changes: 46 additions & 0 deletions test/link-subpath.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"input": "[/relative/link text]",
"tree": {
"elements": [
{
"element": "container",
"data": {
"type": "paragraph",
"attributes": {},
"elements": [
{
"element": "link",
"data": {
"type": "direct",
"link": "/relative/link",
"label": {
"text": "text"
},
"target": null
}
}
]
}
},
{
"element": "footnote-block",
"data": {
"title": null,
"hide": false
}
}
],
"html-blocks": [
],
"code-blocks": [
],
"table-of-contents": [
],
"footnotes": [
],
"bibliographies": [
]
},
"errors": [
]
}
1 change: 1 addition & 0 deletions test/link-triple-anchor.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<wj-body class="wj-body"><p><a href="/scp-series#001" class="wj-link wj-link-internal" data-link-type="page">SCP-001 to SCP-099</a></p></wj-body>
49 changes: 49 additions & 0 deletions test/link-triple-anchor.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"input": "[[[scp-series#001|SCP-001 to SCP-099]]]",
"tree": {
"elements": [
{
"element": "container",
"data": {
"type": "paragraph",
"attributes": {},
"elements": [
{
"element": "link",
"data": {
"type": "page",
"link": {
"site": null,
"page": "scp-series#001"
},
"label": {
"text": "SCP-001 to SCP-099"
},
"target": null
}
}
]
}
},
{
"element": "footnote-block",
"data": {
"title": null,
"hide": false
}
}
],
"html-blocks": [
],
"code-blocks": [
],
"table-of-contents": [
],
"footnotes": [
],
"bibliographies": [
]
},
"errors": [
]
}
1 change: 1 addition & 0 deletions test/link-triple-subpath.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<wj-body class="wj-body"><p><a href="/example/edit" class="wj-link wj-link-internal" data-link-type="page">Edit page</a></p></wj-body>
49 changes: 49 additions & 0 deletions test/link-triple-subpath.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"input": "[[[example/edit|Edit page]]]",
"tree": {
"elements": [
{
"element": "container",
"data": {
"type": "paragraph",
"attributes": {},
"elements": [
{
"element": "link",
"data": {
"type": "page",
"link": {
"site": null,
"page": "example/edit"
},
"label": {
"text": "Edit page"
},
"target": null
}
}
]
}
},
{
"element": "footnote-block",
"data": {
"title": null,
"hide": false
}
}
],
"html-blocks": [
],
"code-blocks": [
],
"table-of-contents": [
],
"footnotes": [
],
"bibliographies": [
]
},
"errors": [
]
}
Loading