Skip to content

Commit

Permalink
Merge pull request #71 from michael-proulx/add-base-url-to-alternate-url
Browse files Browse the repository at this point in the history
Add base url to alternate links
  • Loading branch information
bglw authored Feb 17, 2025
2 parents f9632e9 + 420c24e commit e2b78e7
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 2 deletions.
31 changes: 31 additions & 0 deletions rosey/features/build/rosey-build-meta.feature
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,37 @@ Feature: Rosey Build Meta
| href | /nada/ |
| hreflang | nada |

Scenario: Rosey adds alternates to meta with base url
Given I have a "dist/site/index.html" file with the content:
"""
<html>
</html>
"""
And I have a "dist/site/about.html" file with the content:
"""
<html>
</html>
"""
And I have a "rosey/locales/blank.json" file with the content:
"""
{}
"""
And I have a "rosey/locales/nada.json" file with the content:
"""
{}
"""
When I run my program with the flags:
| build |
| --base-url "https://rosey.app" |
Then I should see a selector 'link' in "dist/translated_site/en/index.html" with the attributes:
| rel | alternate |
| href | https://rosey.app/blank/ |
| hreflang | blank |
Then I should see a selector 'link' in "dist/translated_site/en/index.html" with the attributes:
| rel | alternate |
| href | https://rosey.app/nada/ |
| hreflang | nada |

Scenario: Rosey adds content-language to meta
Given I have a "dist/site/index.html" file with the content:
"""
Expand Down
1 change: 1 addition & 0 deletions rosey/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ impl RoseyOptions {
separator: matches.get("separator", base.separator),
locales: working_dir.join(matches.get("locales", base.locales)),
base: working_dir.join(matches.get("base", base.base)),
base_url: matches.get("base-url", base.base_url),
base_urls: working_dir.join(matches.get("base-urls", base.base_urls)),
default_language: matches.get("default-language", base.default_language),
default_language_at_root: matches.is_present("default-language-at-root") || base.default_language_at_root,
Expand Down
7 changes: 7 additions & 0 deletions rosey/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ async fn main() {
.takes_value(true)
.help("The source folder that Rosey should look for translated images within. \n ─ Defaults to the source folder"),
)
.arg(
Arg::with_name("base-url")
.long("base-url")
.value_name("URL")
.takes_value(true)
.help("The base URL for the site. Used to prefix alternate links. \n ─ Defaults to an empty string"),
)
.arg(
Arg::with_name("default-language-at-root")
.long("default-language-at-root")
Expand Down
3 changes: 3 additions & 0 deletions rosey/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct RoseyPublicConfig {
pub tag: String,
pub separator: String,
pub base: PathBuf,
pub base_url: String,
pub base_urls: PathBuf,
pub locales: PathBuf,
pub languages: Option<Vec<String>>,
Expand All @@ -37,6 +38,7 @@ impl Default for RoseyPublicConfig {
tag: "data-rosey".into(),
separator: ":".into(),
base: "rosey/base.json".into(),
base_url: "".into(),
base_urls: "rosey/base.urls.json".into(),
locales: "rosey/locales".into(),
languages: None,
Expand Down Expand Up @@ -109,6 +111,7 @@ impl Display for RoseyPublicConfig {
writeln!(f, " - Tag: {}", self.tag)?;
writeln!(f, " - Separator: {}", self.separator)?;
writeln!(f, " - Exclusions: {}", self.exclusions)?;
writeln!(f, " - Base url: {}", self.base_url)?;
match &self.languages {
Some(langs) => writeln!(f, " - Languages: {}", langs.join(", "))?,
None => writeln!(
Expand Down
8 changes: 6 additions & 2 deletions rosey/src/runners/builder/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ impl RoseyBuilder {
content,
&config.separator,
&config.tag,
&config.base_url,
images_source.to_owned(),
&config.default_language,
config.default_language_at_root,
Expand Down Expand Up @@ -226,6 +227,7 @@ struct RoseyPage<'a> {
wrap: &'a Option<Vec<String>>,
wrap_class: &'a Option<String>,
pub tag: String,
pub base_url: String,
pub separator: String,
pub images_source: PathBuf,
pub default_language: String,
Expand All @@ -237,6 +239,7 @@ impl<'a> RoseyPage<'a> {
content: String,
separator: &str,
tag: &str,
base_url: &str,
images_source: PathBuf,
default_language: &str,
default_language_at_root: bool,
Expand All @@ -257,6 +260,7 @@ impl<'a> RoseyPage<'a> {
assets: Vec::new(),
separator: separator.to_string(),
tag: tag.to_string(),
base_url: base_url.to_string(),
html_tag: None,
meta_tag: None,
images_source,
Expand Down Expand Up @@ -626,9 +630,9 @@ impl<'a> RoseyPage<'a> {
}

let output_href = if key == self.default_language && self.default_language_at_root {
format!("/{translated_path}")
format!("{0}/{translated_path}", self.base_url)
} else {
format!("/{key}/{translated_path}")
format!("{0}/{key}/{translated_path}", self.base_url)
};

if let Some(href) = attributes.get_mut("href") {
Expand Down

0 comments on commit e2b78e7

Please sign in to comment.