Skip to content

Conversation

@cderv
Copy link
Collaborator

@cderv cderv commented May 19, 2021

This fixes #1155

It seems this is required so that the anchor span stays correctly right after the div of class figure.

@yihui do you have previous experience with this ? Is there another solution ?

tested with

The link correctly send to the top of the header

---
title: "book"
output: 
  bookdown::html_document2: default
---

```{r, echo = FALSE, results='asis'}
cat(stringi::stri_rand_lipsum("10"), sep = "\n\n")
```

```{r fig1, fig.cap = "hello"}
plot(mtcars)
```

See \@ref(fig:fig1)

```{r, echo = FALSE, results='asis'}
cat(stringi::stri_rand_lipsum("10"), sep = "\n\n")
```

@cderv cderv added the next to consider for next release label May 19, 2021
@cderv cderv requested a review from yihui May 19, 2021 10:38
Copy link
Contributor

@yihui yihui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

labs[[i]] = label_prefix(type, sep = ': ')(num)
k = max(figs[figs <= i])
content[k] = paste(c(content[k], sprintf('<span id="%s"></span>', lab)), collapse = '')
content[k] = paste(c(content[k], sprintf('<span style="display:block;" id="%s"></span>', lab)), collapse = '')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if it's okay to use div instead:

Suggested change
content[k] = paste(c(content[k], sprintf('<span style="display:block;" id="%s"></span>', lab)), collapse = '')
content[k] = paste(c(content[k], sprintf('<div id="%s"></div>', lab)), collapse = '')

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have seen some example using a span for other issues(e.g https://stackoverflow.com/a/29123823/3436535)
I don't know if using a div can break something as it is stronger than span for HTML structure, isn't it ?

Also, changing it would break some custom CSS if any user has used a rule based on the span, but it seems unlikely (I don't see why it would be useful)

Question: idealy, adding the id on the img tag would be the best, wouldn't it ? or on the div of class figure ?

Anyway, I think a div could work yes. I did not thought of that.

maybe with visibility = "hidden" ? or anything that could help for Accessibility - I wonder how this empty tag renders.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the difference between an empty span and an empty div is that the former's size is 0 x 0 by default and the latter's size is 100% x 0. An empty span with display: block should be equivalent to an empty div.

Also, changing it would break some custom CSS if any user has used a rule based on the span, but it seems unlikely (I don't see why it would be useful)

That's also what I think.

Question: ideally, adding the id on the img tag would be the best, wouldn't it ? or on the div of class figure ?

Yes, that would be ideal. The div or img might have already got an id from ![image](path){#id}.

maybe with visibility = "hidden" ? or anything that could help for Accessibility - I wonder how this empty tag renders.

I don't know how screen readers would read empty tags, either. According to https://stackoverflow.com/a/32854051/559676, it seems that it's fine to use empty tags for layout instead of content.

I'm not sure if visibility = "hidden".

Copy link
Collaborator Author

@cderv cderv May 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The div or img might have already got an id from ![image](path){#id}.

So it is safer for now to add a special div or span for our id to avoid conflict right ?
We may think more about this if we reimplement the referencing feature based on Lua filter or else.

So let's go with a div ? I am fine with it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it is safer for now to add a special div or span for our id to avoid conflict right ?

That's what I think.

We may think more about this if we reimplement the referencing feature based on Lua filter or else.

My impression is that they already add the id to the figure environment instead of using an invisible anchor span, but I could be wrong (you can double check).

So let's go with a div ? I am fine with it.

Yes, we can revert back to a block span later if we discover any problems.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My impression is that they already add the id to the figure environment instead of using an invisible anchor span, but I could be wrong (you can double check).

They put the id to the img tag if you add an id using link attributes

❯ pandoc -t html4
![cap](https://rmarkdown.rstudio.com/docs/reference/figures/logo.png){#id}
^Z
<div class="figure">
<img src="https://rmarkdown.rstudio.com/docs/reference/figures/logo.png" id="id" alt="" />
<p class="caption">cap</p>
</div>

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay I see. Thanks!

@yihui yihui merged commit 92de8a4 into master May 19, 2021
@yihui yihui deleted the span-block branch May 19, 2021 15:00
@cderv
Copy link
Collaborator Author

cderv commented May 19, 2021

@yihui I have checked what Quarto is doing.

If fact, it adds the id on a wrapping div.

<div id="fig:script" class="quarto-figure quarto-figure-center anchored">
	  <figure class="figure">
		    <p><img src="images/hopr_0107.png" class="img-fluid figure-img"></p>
		    <p></p>
		    <figcaption aria-hidden="true" class="figure-caption">Figure 1.7: When you open an R Script (File &gt; New File &gt; R Script in the menu bar), RStudio creates a fourth pane above the console where you can write and edit your code.</figcaption>
		    <p></p>
	  </figure>
	  <a class="anchorjs-link " aria-label="Anchor" data-anchorjs-icon="" href="#fig:script" style="font: 1em / 1 anchorjs-icons; padding-left: 0.375em;"></a>
</div>

It surely does this for other feature related to classes but this would be another solution for this.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 17, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

next to consider for next release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bookdown cross-reference links pointing below figures

3 participants