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

alternate behaviors based on attribute values - example? #50

Open
emylonas opened this issue Nov 4, 2021 · 1 comment
Open

alternate behaviors based on attribute values - example? #50

emylonas opened this issue Nov 4, 2021 · 1 comment

Comments

@emylonas
Copy link

emylonas commented Nov 4, 2021

I'm trying to write a behavior for <ref> that does one thing if the element has a @type="internal" and another if it has @type="bibl" attribute. I'm modeling it on the <ref> example in CETEIcean. The documentation seems to say that I can't make a second behavior - so I am trying to add a second component to the <ref> behavior.

This is not working, but I'm not sure of how the js script syntax works in this case. My js abilities are so-so. In the example below, the first part [type=internal] is fine. The question is where and how to add the [type=bibl]. And I may just be writing bad javascript...

I've looked for an example to follow but was unable to find one (which doesn't mean that there it's not there.)

``
"ref": [
["[type=internal]", function(elt) {
var linkNum = elt.getAttribute("target").replace(/#A-/,"");
var linkPre = '{{ site.baseurl }}edition/plant';
var link = document.createElement("a");
link.innerHTML = elt.innerHTML;
link.href = linkPre.concat(linkNum);
return link;
}],
["[type=bibl]", ["[target]", ["<a href="$rw@target">",""]]]
]

@hcayless
Copy link
Member

hcayless commented Nov 4, 2021

I think you're almost there. I haven't tested it, but something like

{
  "ref": [
  ["[type=internal]", function(elt) {
    let linkNum = elt.getAttribute("target").replace(/#A-/,"");
    let linkPre = '{{ site.baseurl }}edition/plant';
    let link = document.createElement("a");
    link.innerHTML = elt.innerHTML;
    link.href = linkPre.concat(linkNum);
    return link;
  }],
  ["[type=bibl][target]", ["<a href=\"$rw@target\">","</a>"]]
  ]
}

should work. Whether the {{ site.baseurl }} bit works may depend on when and how your templating engine processes things. The [type=bibl][target] is a CSS selector, and that syntax ought to work for multiple attributes.

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

No branches or pull requests

2 participants