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

content: Handle code block spans with multiple CSS classes #1021

Merged
merged 1 commit into from
Oct 25, 2024

Conversation

rajveermalviya
Copy link
Member

Fixes: #933

Flutter Web
Screenshot 2024-10-23 at 20 35 38 Screenshot 2024-10-23 at 20 35 54

@gnprice gnprice added the maintainer review PR ready for review by Zulip maintainers label Oct 23, 2024
@PIG208 PIG208 self-requested a review October 24, 2024 01:50
@@ -1023,17 +1023,28 @@ class _ZulipContentParser {
span = CodeBlockSpanNode(text: text, type: CodeBlockSpanType.text);

case dom.Element(localName: 'span', :final text, :final className):
Copy link
Member

@PIG208 PIG208 Oct 24, 2024

Choose a reason for hiding this comment

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

nit: can use dom.Element.classes here, so we don't need to call .split(' ') ourselves later

@rajveermalviya rajveermalviya force-pushed the pr-code-block-multiple-classes branch from e37129c to cafabca Compare October 24, 2024 16:41
@rajveermalviya
Copy link
Member Author

Thanks for the review @PIG208! Pushed a new revision, PTAL :)

@PIG208 PIG208 added integration review Added by maintainers when PR may be ready for integration and removed maintainer review PR ready for review by Zulip maintainers labels Oct 24, 2024
@PIG208 PIG208 assigned gnprice and unassigned PIG208 Oct 24, 2024
@PIG208 PIG208 requested a review from gnprice October 24, 2024 17:27
@PIG208
Copy link
Member

PIG208 commented Oct 24, 2024

LGTM!

Copy link
Member

@gnprice gnprice left a comment

Choose a reason for hiding this comment

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

Thanks @rajveermalviya for taking care of this, and @PIG208 for the previous review!

Generally this looks great. Small comments below, most of them nits.

final CodeBlockSpanType type = codeBlockSpanTypeFromClassName(className);
switch (type) {
case CodeBlockSpanType.unknown:
case dom.Element(localName: 'span', :final text, :final classes):
Copy link
Member

Choose a reason for hiding this comment

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

In the parser we should stick to className, avoiding classes 🙂 — see #497.

Comment on lines 1032 to 1034
final spanType =
classes
.map(codeBlockSpanTypeFromClassName)
Copy link
Member

Choose a reason for hiding this comment

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

nit: tighten formatting by putting part of the initializer on the first line, after the =

Comment on lines 1026 to 1030
// The general form for Pygments nodes with multiple classes observed
// are, that the first class is the standard token class and trailing
// tokens are non-standard tokens. Zulip web client only styles the
// standard tokens classes. Same is done here, only standard token
// class names are emitted.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// The general form for Pygments nodes with multiple classes observed
// are, that the first class is the standard token class and trailing
// tokens are non-standard tokens. Zulip web client only styles the
// standard tokens classes. Same is done here, only standard token
// class names are emitted.
// Empirically, when a Pygments node has multiple classes, the first
// class names a standard token type and the rest are for non-standard
// token types specific to the language. Zulip web only styles the
// standard token classes and ignores the others, so we do the same.

The "are emitted" confused me: we don't emit any class names here, right? We consume class names, and may emit a CodeBlockSpanType value.

Comment on lines 403 to 408
'<div class="codehilite" data-code-language="YAML">'
'<pre><span></span><code><span class="p p-Indicator">-</span>'
'<span class="w"> </span>'
'<span class="l l-Scalar l-Scalar-Plain">item</span>\n'
'</code></pre></div>', [
CodeBlockNode([
Copy link
Member

Choose a reason for hiding this comment

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

super nit: indent the string continuations deeper so they don't look like part of the same series as the list element below

Suggested change
'<div class="codehilite" data-code-language="YAML">'
'<pre><span></span><code><span class="p p-Indicator">-</span>'
'<span class="w"> </span>'
'<span class="l l-Scalar l-Scalar-Plain">item</span>\n'
'</code></pre></div>', [
CodeBlockNode([
'<div class="codehilite" data-code-language="YAML">'
'<pre><span></span><code><span class="p p-Indicator">-</span>'
'<span class="w"> </span>'
'<span class="l l-Scalar l-Scalar-Plain">item</span>\n'
'</code></pre></div>', [
CodeBlockNode([

This is similar to what we do with if conditions. You can also see it in action on the similar examples codeBlockHighlightedShort and codeBlockHighlightedMultiline above.

@@ -395,6 +395,23 @@ class ContentExample {
ParagraphNode(links: null, nodes: [TextNode("some content")]),
]);

static const codeBlockNodesWithMultipleClasses = ContentExample(
Copy link
Member

Choose a reason for hiding this comment

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

nit: put this just after the two codeBlockHighlighted… examples, so it's not separated from them by all the unimplemented code-block examples

@rajveermalviya rajveermalviya force-pushed the pr-code-block-multiple-classes branch from cafabca to 4a70436 Compare October 25, 2024 04:38
@rajveermalviya
Copy link
Member Author

Thanks for the reviews @PIG208, @gnprice!
@gnprice Pushed a new revision, PTAL.

Copy link
Member

@gnprice gnprice left a comment

Choose a reason for hiding this comment

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

Thanks for the revision! Looks good, with just one comment below on something I missed earlier.

Comment on lines 347 to 348
static const codeBlockNodesWithMultipleClasses = ContentExample(
'code block nodes with multiple class names',
Copy link
Member

Choose a reason for hiding this comment

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

Rereading, I realized this name is misleading: "code block node" should correspond CodeBlockNode, since we have such an identifier; but the HTML element that corresponds to one of those is the whole div.codehilite. The things that have multiple classes are the span great-grandchildren of that, which correspond to CodeBlockSpanNode.

I think "code block span" is a fine term for these.

So that applies to this example's name and description, and to the commit message.

@rajveermalviya rajveermalviya force-pushed the pr-code-block-multiple-classes branch from 4a70436 to 4224f4b Compare October 25, 2024 05:51
@rajveermalviya rajveermalviya changed the title content: Handle multiple class names in code block nodes content: Handle code block spans with multiple CSS classes Oct 25, 2024
@rajveermalviya
Copy link
Member Author

Thanks for the review @gnprice. Revision pushed.

@gnprice
Copy link
Member

gnprice commented Oct 25, 2024

Thanks! All looks good — merging.

@gnprice gnprice force-pushed the pr-code-block-multiple-classes branch from 4224f4b to e3d5471 Compare October 25, 2024 22:58
@gnprice gnprice merged commit e3d5471 into zulip:main Oct 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
integration review Added by maintainers when PR may be ready for integration
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Handle codehilite elements with multiple classes
3 participants