Skip to content

Feature: Add syntax highlighting support for non-executable snippets#1159

Open
maptoan wants to merge 1 commit intosource-academy:masterfrom
maptoan:contribai/feat/add-syntax-highlighting-support-for-non-
Open

Feature: Add syntax highlighting support for non-executable snippets#1159
maptoan wants to merge 1 commit intosource-academy:masterfrom
maptoan:contribai/feat/add-syntax-highlighting-support-for-non-

Conversation

@maptoan
Copy link
Copy Markdown

@maptoan maptoan commented Mar 30, 2026

Problem

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.

Severity: medium
File: javascript/processingFunctions/processSnippetHtml.js

Solution

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.

Changes

  • javascript/processingFunctions/processSnippetHtml.js (modified)

Testing

  • Existing tests pass
  • Manual review completed
  • No new warnings/errors introduced

Closes #757

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>
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

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>");
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

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>");
}
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WEB: syntax highlighting broken...

1 participant