From dcc603ed240c2422fa4c512bd263a5961269cbe3 Mon Sep 17 00:00:00 2001 From: Liam Bigelow <40188355+bglw@users.noreply.github.com> Date: Fri, 1 Nov 2024 10:41:10 +1300 Subject: [PATCH 1/2] Add Rosey tag to ignore rewriting hrefs --- docs/content/docs/urls.md | 19 +++++++++++--- .../features/build/rosey-build-links.feature | 26 +++++++++++++++++++ rosey/src/runners/builder/html.rs | 3 +++ 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/docs/content/docs/urls.md b/docs/content/docs/urls.md index 596c4e5..2417b80 100644 --- a/docs/content/docs/urls.md +++ b/docs/content/docs/urls.md @@ -1,15 +1,28 @@ --- -title: "Translated URLs" -nav_title: "Translated URLs" +title: "Managing URLs" +nav_title: "Managing URLs" nav_section: Workflow weight: 6 --- -Rosey URL locale files can contain translated URLs for your website in a given language. +Rosey provides some options for managing URLs. +Rosey can ignore some URLs altogether, and Rosey URL locale files can contain translated URLs for your website in a given language. If you just want to move one language to the root of the site, e.g. serve `/en/index.html` at `index.html` instead, see the [Default language at root](/docs/build/#default-language-at-root) option for Rosey's build step. +## Telling Rosey to ignore URLs + +If you have a URL on a page that you do not want Rosey to rewrite, you can add the `data-rosey-ignore` attribute to it. + +```html + Posts + + Contact +``` + +When built to a page in an `fr` locale, the first tag would link to `/fr/posts/`, while the tag with `data-rosey-ignore` would remain pointing at `/contact/`. + ## Creating translated URL locale files Creating URL locale files is not a step performed by Rosey. This part of the translation workflow is left open ended, usually integrating into an existing translation workflow for a company, or being programmatically created by transforming the input URLs. diff --git a/rosey/features/build/rosey-build-links.feature b/rosey/features/build/rosey-build-links.feature index a08e6b9..873c6ff 100644 --- a/rosey/features/build/rosey-build-links.feature +++ b/rosey/features/build/rosey-build-links.feature @@ -74,3 +74,29 @@ Feature: Rosey Links Then I should see a selector 'h2>a' in "dist/translated_site/blank/index.html" with the attributes: | href | /blank/hello | | innerText | Hello | + + + Scenario: Rosey can ignore links + Given I have a "dist/site/index.html" file with the content: + """ + + +

Home

+

Hello World

+ + + """ + And I have a "rosey/locales/blank.json" file with the content: + """ + {} + """ + When I run my program with the flags: + | build | + Then I should see a selector 'h1>a' in "dist/translated_site/blank/index.html" with the attributes: + | href | / | + | data-rosey-ignore | | + | innerText | Home | + Then I should see a selector 'h2>a' in "dist/translated_site/blank/index.html" with the attributes: + | href | /posts/hello-world | + | data-rosey-ignore | | + | innerText | Hello World | diff --git a/rosey/src/runners/builder/html.rs b/rosey/src/runners/builder/html.rs index 4a42839..dbdb843 100644 --- a/rosey/src/runners/builder/html.rs +++ b/rosey/src/runners/builder/html.rs @@ -296,6 +296,9 @@ impl<'a> RoseyPage<'a> { for element in self.dom.select("a[href]").unwrap() { let attributes = element.attributes.borrow(); + if attributes.contains(format!("{}-ignore", self.tag)) { + continue; + } let src = attributes.get("href").unwrap(); let Ok(parsed) = base_url.join(src) else { continue; From 1ba6ac8bdaff16800b65d00788d88b6da5d0fe20 Mon Sep 17 00:00:00 2001 From: Liam Bigelow <40188355+bglw@users.noreply.github.com> Date: Fri, 1 Nov 2024 10:43:44 +1300 Subject: [PATCH 2/2] Changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0469d8d..8feb84d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,9 @@ ## Unreleased +* Added `data-rosey-ignore` attribute to ignore hrefs when rewriting pages. + * See [Docs > URLS](https://rosey.app/docs/urls/) for more information. + ## v2.2.1 (August 28, 2024) * Ensures the JSON output of the check command is sorted to avoid changes in git between runs