-
-
Notifications
You must be signed in to change notification settings - Fork 887
fix: relative links in gleam toml #5076
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -980,11 +980,44 @@ pub struct DocsPage { | |
| pub source: Utf8PathBuf, | ||
| } | ||
|
|
||
| #[derive(Deserialize, Serialize, Debug, PartialEq, Eq, Clone)] | ||
| #[serde(untagged)] | ||
| pub enum Href { | ||
| #[serde(with = "uri_serde")] | ||
| External(Uri), | ||
| Internal(std::path::PathBuf), | ||
| } | ||
|
|
||
| impl fmt::Display for Href { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| match self { | ||
| Href::External(uri) => write!(f, "{}", uri), | ||
| Href::Internal(path) => write!(f, "{}", path.display()), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Href { | ||
| pub fn is_internal(&self) -> bool { | ||
| match self { | ||
| Href::Internal(_) => true, | ||
| Href::External(_) => false, | ||
| } | ||
| } | ||
|
|
||
| // only URIs are considered as external links | ||
| pub fn as_uri(&self) -> Option<Uri> { | ||
| match self { | ||
| Href::External(uri) => Some(uri.clone()), | ||
| Href::Internal(_) => None, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Deserialize, Serialize, Debug, PartialEq, Eq, Clone)] | ||
| pub struct Link { | ||
| pub title: String, | ||
| #[serde(with = "uri_serde")] | ||
| pub href: Uri, | ||
| pub href: Href, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Href type here looks like it'll permit data that's not valid URLs. How about instead we use the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I read through Many contexts allow URL references that can be relative to a base URL: <link rel="stylesheet" href="../main.css">Since parsed URLs are absolute, giving a base is required for parsing relative URLs: use url::{Url, ParseError};
assert!(Url::parse("../main.css") == Err(ParseError::RelativeUrlWithoutBase))Use the join method on an Url to use it as a base URL: use url::Url;
let this_document = Url::parse("http://servo.github.io/rust-url/url/index.html")?;
let css_url = this_document.join("../main.css")?;
assert_eq!(css_url.as_str(), "http://servo.github.io/rust-url/main.css");I think the main problem with my implementation is that recursive paths are valid, which should only be the case if the "base url" provides a path (e.g. http://myhexdoc.com/foo/bar). What I could do is to check if the user provided an "Home Page" within the link list, if so it will use that home page link as the base url. If not then I could fall back to a strict generic base url without path like What do you think about that strategy ? |
||
| } | ||
|
|
||
| // Note we don't use http-serde since we also want to validate the scheme and host is set. | ||
|
|
@@ -1141,7 +1174,7 @@ licences = ["Apache-2.0", "MIT"] | |
| description = "Pretty complex config" | ||
| target = "erlang" | ||
| repository = { type = "github", user = "example", repo = "my_dep" } | ||
| links = [{ title = "Home page", href = "https://example.com" }] | ||
| links = [{ title = "Home page", href = "https://example.com" }, { title = "Internal link", href = "./internal" }, { title = "Second internal link", href = "../internal" }, { title = "Third internal link", href = "internal" } ] | ||
| internal_modules = ["my_app/internal"] | ||
| gleam = ">= 0.30.0" | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Put the filter before the mapping and remove the
cloneplease 🙏