feat(viewer): inline HCL/Terraform syntax highlighting#11
Open
vnz wants to merge 1 commit intoCSCSoftware:masterfrom
Open
feat(viewer): inline HCL/Terraform syntax highlighting#11vnz wants to merge 1 commit intoCSCSoftware:masterfrom
vnz wants to merge 1 commit intoCSCSoftware:masterfrom
Conversation
The viewer loads highlight.js from CDN, which doesn't bundle an HCL
language module. Files with `.tf`, `.tfvars`, `.hcl` extensions previously
fell back to plaintext rendering.
Register a minimal HCL grammar inline (in the page's <script>) covering:
- Block keywords: resource, variable, module, output, locals, data,
provider, terraform, etc.
- Meta-arguments: count, for_each, depends_on, etc.
- Type constraints: string, number, bool, list, map, etc.
- Built-in functions: lookup, merge, file, jsonencode, length, etc.
- Hash, line, and block comments
- Strings with `${...}` interpolation
- Heredoc strings (<<EOF and <<-EOF)
- Numbers
- Function call recognition for the title.function class
Switches the viewer's extension map for `.tf`/`.tfvars`/`.hcl` from
'plaintext' back to 'hcl' now that the language is registered.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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.
Summary
Adds HCL/Terraform syntax highlighting in the Viewer. Previously
.tf/.tfvars/.hclfiles rendered as plaintext because highlight.js's CDN bundle doesn't include an HCL grammar module.Approach
Register a minimal inline HCL grammar in the viewer's HTML head, right after the highlight.js script tag. No new dependency — the grammar is ~50 lines of JavaScript that uses highlight.js's existing APIs (
hljs.registerLanguage,hljs.HASH_COMMENT_MODE,hljs.NUMBER_MODE, etc.).The grammar covers:
resource,variable,module,output,locals,data,provider,terraform,backend,provisioner,connection,lifecycle,dynamic, etc.count,for_each,depends_on,providers,source,versionstring,number,bool,list,map,set,object,tuple,any,optionaltrue,false,nulllookup,merge,file,jsonencode,length, etc.) — get thebuilt_inhighlight class#,//,/* */${var.foo}interpolation assubstclass<<EOFand<<-EOFindented formname(matched astitle.functionclassAlso flips the viewer's extension map for
.tf/.tfvars/.hclfrom'plaintext'back to'hcl'now that the language is registered.Why inline instead of a dependency
@taga3s/highlightjs-terraformwould need to be bundled into the viewer build and served — that's a build-system change for ~50 lines of grammarNotes on regex escaping
The viewer's HTML is generated via a TypeScript template literal. Each
\in a runtime regex needs\\\\in the source (template literal eats one layer, JS string parser eats another). All regex patterns are written as strings ('\\\\$\\\\{') rather than regex literals (/\\$\\{/) to avoid template-literal quirks with\$and\{(template literals interpret\$as escape for interpolation).Test plan
.tffile — verify keywords likeresource,variable,moduleare colored${var.environment}interpolation is highlighted as a substitution# ...) are styled as commentstemplatefile(...),lookup(...)get the function class🤖 Generated with Claude Code