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

Alternates are not generated, and no docs for that functionality #41

Open
yznts opened this issue Feb 16, 2021 · 6 comments · May be fixed by #45
Open

Alternates are not generated, and no docs for that functionality #41

yznts opened this issue Feb 16, 2021 · 6 comments · May be fixed by #45

Comments

@yznts
Copy link

yznts commented Feb 16, 2021

I'm trying to handle language routes in my generator script.
Checked your sources and found that "alternates" keyword, so tried to do in that way:

image

It's not working at all, and I can't find any functionality in the code related to alternates generation.

@nlappe
Copy link

nlappe commented Feb 23, 2021

Can confirm. i just stumbled into the same situation.

Could you solve the issue?
using alternates doesnt throw an error but rather is completely ignored and the XML is rendered fine - just without the xhtml:link tags

@yznts
Copy link
Author

yznts commented Feb 23, 2021

@nlappe
Not yet. Going to make PR in a while if there would be no updates on that

@nlappe
Copy link

nlappe commented Feb 27, 2021

@yuriizinets
Thanks for the update.
I had to fall back to another library and decided to just build the XML myself.
If you're in a hurry (which i was for the sitemap) you can look into the dependencies of this lib - the required github.com/beevik/etree package is super easy to use.

@yznts
Copy link
Author

yznts commented Mar 9, 2021

@nlappe Sorry, I'm not going to make a PR because I'm not so familiar with this codebase and project is not maintained, as I see now. I contacted with project owner without success.

As a possible solution/workaround:

  • Clone go-sitemap-generator into your project directory and make it local module with replace feature of go.mod
  • In the go-sitemap-generator/stm/builder_url.go method XML() add SetBuilderElementValue(url, su.data, "xhtml:link") above SetBuilderElementValue(url, su.data, "expires")
  • In the same file, go to the validate() method and add this after the line 72. In that way we will allow xhtml:link keyword (I had 2 approaches: replace alternates with xhtml:link inside of library in runtime, or just use xhtml:link everywhere, so I've implemented second option)
if key == "xhtml:link" {
    invalid = false
    break
}

Final usage:

// Alternates are []Attr (!!!)
sm.Add(stm.URL{{"loc", url}, {"xhtml:link", alternates}})

@nlappe
Copy link

nlappe commented Mar 11, 2021

@yuriizinets Thanks for your Feedback.
As time did matter i chose to implement the feature myself with the help of github.com/beevik/etree. (which is a dependency of this package)

You can do something like
alternate := [instsance of an *etree.Element].CreateElement("xhtml:link")
alternate.CreateAttr("rel", "alternate")
alternate.CreateAttr("hreflang", "<lang>")
alternate.CreateAttr("href", "<url>")
to achieve the same.

It works like a Charme :)

If you ever have to work with XML type data in go its a nice starting point.

This approach also offers a bit more flexibility for future things.

@yznts
Copy link
Author

yznts commented Mar 11, 2021

@nlappe used this package to avoid direct work with XML, but... 😄
Thanks for notes!

@henrymwang henrymwang linked a pull request Dec 16, 2021 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants