-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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 support for image, rules and footnotes #40919
Conversation
/// [link]: https://example.com "this is a title" | ||
/// | ||
/// hard break: | ||
/// after hard break |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this generates a sequence like:
[..., Text("hard break:"), SoftBreak, Text("after hard break"), ...]
To generate a HardBreak
in place of SoftBreak
, there needs to be at least 2 trailing spaces right before the newline character:
"hard break \nafter hard break"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The issue is even before the markdown parsing, a trim
is being done somewhere so the text the parser receives never has lines ending with whitespaces.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a test for it. Should be fully working now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Footnote implementation is incomplete.
src/librustdoc/html/markdown.rs
Outdated
let cur_len = parser.footnotes.len() + 1; | ||
parser.footnotes.push(format!("<li id=\"ref{}\">{}<a href=\"#supref{0}\" \ | ||
rev=\"footnote\">↩</a></li>", | ||
cur_len, content)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is wrong. For example:
foot[^note1], foot[^note2]
[^note1]: text1
[^note2]: text2
Will give the following event sequence:
[
Start(Paragraph),
Text("foot"),
FootnoteReference("note1"),
Text(", foot"),
FootnoteReference("note2"),
End(Paragraph),
Start(FootnoteDefinition("note1")),
Start(Paragraph),
Text("text1"),
End(Paragraph),
End(FootnoteDefinition("note1"))
Start(FootnoteDefinition("note2")),
Start(Paragraph),
Text("text2"),
End(Paragraph),
End(FootnoteDefinition("note2"))
]
Correct me if I am wrong, but according to this code snippet, both footnote definitions will have the same id cur_len
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh absolutely! Thanks for notifying it!
src/librustdoc/html/markdown.rs
Outdated
Event::FootnoteReference(_) => { | ||
buffer.push_str(&format!("<sup id=\"supref{0}\"><a href=\"#ref{0}\">{0}</a>\ | ||
</sup>", | ||
parser.get_next_footnote_id())); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Footnote ids cannot be this simple either. The same footnote can be referenced from multiple places. So you can't simply keep incrementing the id whenever a footnote reference is found. You'd need to use a map.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, I'll update to this.
src/librustdoc/html/markdown.rs
Outdated
id); | ||
let cur_len = parser.footnotes.len() + 1; | ||
parser.footnotes.push(format!("<li id=\"ref{}\">{}<a href=\"#supref{0}\" \ | ||
rev=\"footnote\">↩</a></li>", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is parent ul
generated?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At the end.
&content[..content.len() - 4] | ||
} else { | ||
&content | ||
})); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's one more issue here, though it would be very rarely encountered, if at all. When there are multiple footnote references, there needs to be multiple backreferences from the definition too, to be 100% proper. (For a nice reference implementation, see Wikipedia.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, for now let's keep it aside. This PR is already big enough.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(But it's a great suggestion!)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, the current footnote implementation does this. I think @GuillaumeGomez is right though, we can add that back in in another PR; it's not as big of a deal as this stuff is.
4911564
to
4de4a95
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this looks good. Let's see what @frewsxcv says.
@@ -115,15 +116,19 @@ macro_rules! event_loop_break { | |||
match event { | |||
$($end_event)|* => break, | |||
Event::Text(ref s) => { | |||
debug!("Text"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
was this meant to be left in?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not opposed to keeping the debugging statements, but it'd be nicer if there were a little more descriptive, maybe something like debug!("markdown parser encountered 'Text'")
, whatever works though.
Event::SoftBreak | Event::HardBreak if !$buf.is_empty() => { | ||
$buf.push(' '); | ||
Event::SoftBreak => { | ||
debug!("SoftBreak"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here
pub fn render(w: &mut fmt::Formatter, | ||
s: &str, | ||
print_toc: bool, | ||
shorter: MarkdownOutputStyle) -> fmt::Result { | ||
fn code_block(parser: &mut Parser, buffer: &mut String, lang: &str) { | ||
fn code_block(parser: &mut ParserWrapper, buffer: &mut String, lang: &str) { | ||
debug!("CodeBlock"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I won't comment on all of these 😄
&content[..content.len() - 4] | ||
} else { | ||
&content | ||
})); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, the current footnote implementation does this. I think @GuillaumeGomez is right though, we can add that back in in another PR; it's not as big of a deal as this stuff is.
|
||
// @has foo/fn.f.html | ||
// @has - '<p>hard break:<br>after hard break</p>' | ||
/// hard break: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just a review note; this works because there are the two trailing spaces. I was confused at first 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's the point of hard break. :p
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I just meant it's hard to see in this diff.
This PR is now ready. |
Looks great! r=me Not going to r+ right now in case you want to change/remove the debug logging statements. |
As I said to @steveklabnik, it'll certainly be helpful in a close future. Once this is all done, we can remove them. @bors: r+ |
📌 Commit ef01ae7 has been approved by |
@bors r=frewsxcv,steveklabnik |
💡 This pull request was already approved, no need to approve it again.
|
📌 Commit ef01ae7 has been approved by |
…veklabnik Add support for image, rules and footnotes Part of #40912. r? @rust-lang/docs PS: the footnotes are waiting for pulldown-cmark/pulldown-cmark#21 to be merged to be fully working.
☀️ Test successful - status-appveyor, status-travis |
Part of #40912.
r? @rust-lang/docs
PS: the footnotes are waiting for pulldown-cmark/pulldown-cmark#21 to be merged to be fully working.