Skip to content

Commit

Permalink
Add Rosey tag to ignore rewriting hrefs
Browse files Browse the repository at this point in the history
  • Loading branch information
bglw committed Oct 31, 2024
1 parent 601e609 commit dcc603e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
19 changes: 16 additions & 3 deletions docs/content/docs/urls.md
Original file line number Diff line number Diff line change
@@ -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
<a href="/posts/"> Posts </a>

<a data-rosey-ignore href="/contact/"> Contact </a>
```

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.
Expand Down
26 changes: 26 additions & 0 deletions rosey/features/build/rosey-build-links.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
<html>
<body>
<h1><a data-rosey-ignore href="/">Home</a></h1>
<h2><a data-rosey-ignore href="/posts/hello-world">Hello World</a></h2>
</body>
</html>
"""
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 |
3 changes: 3 additions & 0 deletions rosey/src/runners/builder/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit dcc603e

Please sign in to comment.