Feature: Add syntax highlighting support for non-executable snippets#1159
Open
maptoan wants to merge 1 commit intosource-academy:masterfrom
Open
Conversation
The current implementation of `processSnippetHtml` only applies syntax highlighting classes to snippets that have specific execution tags. This leaves static code blocks unstyled. I will modify the function to ensure that all code snippets, including those that are purely illustrative, are wrapped in the `<pre class="prettyprint">` tag (or equivalent) so that the CSS can apply syntax highlighting. Affected files: processSnippetHtml.js Signed-off-by: toanmap <174589430+maptoan@users.noreply.github.com>
There was a problem hiding this comment.
Code Review
This pull request updates the CSS class name for JavaScript snippets from 'prettyprintoutput' to 'prettyprint' to ensure consistent styling. A review comment suggests refactoring the repeated logic for handling different snippet tags into a single loop to reduce code duplication and improve maintainability.
Comment on lines
64
to
78
| const jsPromptSnippet = node.getElementsByTagName("JAVASCRIPT_PROMPT")[0]; | ||
|
|
||
| if (jsPromptSnippet) { | ||
| writeTo.push("<pre class='prettyprintoutput'>"); | ||
| writeTo.push("<pre class='prettyprint'>"); | ||
| writeTo.push(jsPromptSnippet.firstChild.nodeValue.trimRight()); | ||
| writeTo.push("</pre>"); | ||
| } | ||
|
|
||
| const jsLonelySnippet = node.getElementsByTagName("JAVASCRIPT_LONELY")[0]; | ||
|
|
||
| if (jsLonelySnippet) { | ||
| writeTo.push("<pre class='prettyprintoutput'>"); | ||
| writeTo.push("<pre class='prettyprint'>"); | ||
| writeTo.push(jsLonelySnippet.firstChild.nodeValue.trimRight()); | ||
| writeTo.push("</pre>"); | ||
| } |
There was a problem hiding this comment.
While your change is functionally correct, there's an opportunity to refactor the code to reduce duplication. The logic for handling JAVASCRIPT_PROMPT and JAVASCRIPT_LONELY is identical. You can combine them into a loop to make the code more concise and maintainable.
Suggested change
| const jsPromptSnippet = node.getElementsByTagName("JAVASCRIPT_PROMPT")[0]; | |
| if (jsPromptSnippet) { | |
| writeTo.push("<pre class='prettyprintoutput'>"); | |
| writeTo.push("<pre class='prettyprint'>"); | |
| writeTo.push(jsPromptSnippet.firstChild.nodeValue.trimRight()); | |
| writeTo.push("</pre>"); | |
| } | |
| const jsLonelySnippet = node.getElementsByTagName("JAVASCRIPT_LONELY")[0]; | |
| if (jsLonelySnippet) { | |
| writeTo.push("<pre class='prettyprintoutput'>"); | |
| writeTo.push("<pre class='prettyprint'>"); | |
| writeTo.push(jsLonelySnippet.firstChild.nodeValue.trimRight()); | |
| writeTo.push("</pre>"); | |
| } | |
| ["JAVASCRIPT_PROMPT", "JAVASCRIPT_LONELY"].forEach(tagName => { | |
| const snippet = node.getElementsByTagName(tagName)[0]; | |
| if (snippet) { | |
| writeTo.push("<pre class='prettyprint'>"); | |
| writeTo.push(snippet.firstChild.nodeValue.trimRight()); | |
| writeTo.push("</pre>"); | |
| } | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The current implementation of
processSnippetHtmlonly applies syntax highlighting classes to snippets that have specific execution tags. This leaves static code blocks unstyled. I will modify the function to ensure that all code snippets, including those that are purely illustrative, are wrapped in the<pre class="prettyprint">tag (or equivalent) so that the CSS can apply syntax highlighting.Severity:
mediumFile:
javascript/processingFunctions/processSnippetHtml.jsSolution
The current implementation of
processSnippetHtmlonly applies syntax highlighting classes to snippets that have specific execution tags. This leaves static code blocks unstyled. I will modify the function to ensure that all code snippets, including those that are purely illustrative, are wrapped in the<pre class="prettyprint">tag (or equivalent) so that the CSS can apply syntax highlighting.Changes
javascript/processingFunctions/processSnippetHtml.js(modified)Testing
Closes #757