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

Add Rosey tag to ignore rewriting hrefs #66

Merged
merged 2 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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
Loading