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

Improve rendering of <code> tags in documentation #3411

Closed
A-Walrus opened this issue Aug 13, 2022 · 1 comment · Fixed by #3425
Closed

Improve rendering of <code> tags in documentation #3411

A-Walrus opened this issue Aug 13, 2022 · 1 comment · Fixed by #3425
Labels
A-helix-term Area: Helix term improvements C-enhancement Category: Improvements

Comments

@A-Walrus
Copy link
Contributor

Describe your feature request

Documentation, at least for rust, can contain <code> tags, for example the documentation for into() from the standard library. The tags themselves should not be shown, and the text within should be shown the same as text within "`":
image
The <code> and </code> tags should not be shown and the text within them should be styled the same as the U::from(self) from above.

@A-Walrus A-Walrus added the C-enhancement Category: Improvements label Aug 13, 2022
@the-mikedavis the-mikedavis added the A-helix-term Area: Helix term improvements label Aug 13, 2022
@the-mikedavis
Copy link
Member

Markdown rendering for things like doc popups is handled here:

for event in parser {
match event {
Event::Start(Tag::List(list)) => {
// if the list stack is not empty this is a sub list, in that
// case we need to push the current line before proceeding
if !list_stack.is_empty() {
push_line(&mut spans, &mut lines);
}
list_stack.push(list);
}
Event::End(Tag::List(_)) => {
list_stack.pop();
// whenever top-level list closes, empty line
if list_stack.is_empty() {
lines.push(Spans::default());
}
}
Event::Start(Tag::Item) => {
if list_stack.is_empty() {
log::warn!("markdown parsing error, list item without list");
}
tags.push(Tag::Item);
// get the appropriate bullet for the current list
let bullet = list_stack
.last()
.unwrap_or(&None) // use the '- ' bullet in case the list stack would be empty
.map_or(String::from("- "), |number| format!("{}. ", number));
// increment the current list number if there is one
if let Some(v) = list_stack.last_mut().unwrap_or(&mut None).as_mut() {
*v += 1;
}
let prefix = get_indent(list_stack.len()) + bullet.as_str();
spans.push(Span::from(prefix));
}
Event::Start(tag) => {
tags.push(tag);
if spans.is_empty() && !list_stack.is_empty() {
// TODO: could push indent + 2 or 3 spaces to align with
// the rest of the list.
spans.push(Span::from(get_indent(list_stack.len())));
}
}
Event::End(tag) => {
tags.pop();
match tag {
Tag::Heading(_, _, _)
| Tag::Paragraph
| Tag::CodeBlock(CodeBlockKind::Fenced(_))
| Tag::Item => {
push_line(&mut spans, &mut lines);
}
_ => (),
}
// whenever heading, code block or paragraph closes, empty line
match tag {
Tag::Heading(_, _, _)
| Tag::Paragraph
| Tag::CodeBlock(CodeBlockKind::Fenced(_)) => {
lines.push(Spans::default());
}
_ => (),
}
}
Event::Text(text) => {
// TODO: temp workaround
if let Some(Tag::CodeBlock(CodeBlockKind::Fenced(language))) = tags.last() {
let tui_text = highlighted_code_block(
text.to_string(),
language,
theme,
Arc::clone(&self.config_loader),
None,
);
lines.extend(tui_text.lines.into_iter());
} else {
let style = if let Some(Tag::Heading(level, ..)) = tags.last() {
match level {
HeadingLevel::H1 => heading_styles[0],
HeadingLevel::H2 => heading_styles[1],
HeadingLevel::H3 => heading_styles[2],
HeadingLevel::H4 => heading_styles[3],
HeadingLevel::H5 => heading_styles[4],
HeadingLevel::H6 => heading_styles[5],
}
} else {
text_style
};
spans.push(Span::styled(text, style));
}
}
Event::Code(text) | Event::Html(text) => {
spans.push(Span::styled(text, code_style));
}
Event::SoftBreak | Event::HardBreak => {
push_line(&mut spans, &mut lines);
if !list_stack.is_empty() {
// TODO: could push indent + 2 or 3 spaces to align with
// the rest of the list.
spans.push(Span::from(get_indent(list_stack.len())));
}
}
Event::Rule => {
lines.push(Spans::from(Span::styled("---", code_style)));
lines.push(Spans::default());
}
// TaskListMarker(bool) true if checked
_ => {
log::warn!("unhandled markdown event {:?}", event);
}
}
// build up a vec of Paragraph tui widgets
}

For this snippet, the events in that loop look like

Start(CodeBlock(Fenced(Borrowed("rust"))))
Text(Borrowed("core::convert\n"))
End(CodeBlock(Fenced(Borrowed("rust"))))
Start(CodeBlock(Fenced(Borrowed("rust"))))
Text(Borrowed("fn into(self) -> U\n"))
End(CodeBlock(Fenced(Borrowed("rust"))))
Rule
Start(Paragraph)
Text(Borrowed("Calls "))
Code(Borrowed("U::from(self)"))
Text(Borrowed("."))
End(Paragraph)
Start(Paragraph)
Text(Borrowed("That is, this conversion is whatever the implementation of"))
SoftBreak
Html(Borrowed("<code>"))
SoftBreak
Start(Link(Inline, Borrowed("https://doc.rust-lang.org/nightly/core/convert/trait.From.html"), Borrowed("")))
Text(Borrowed("From"))
End(Link(Inline, Borrowed("https://doc.rust-lang.org/nightly/core/convert/trait.From.html"), Borrowed("")))
Text(Borrowed("<T"))
Text(Borrowed("> for U"))
Html(Borrowed("</code>"))
Text(Borrowed(" chooses to do."))
End(Paragraph)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-helix-term Area: Helix term improvements C-enhancement Category: Improvements
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants