{"id":13483556,"url":"https://github.com/shaunlebron/highlight-tree-sitter","last_synced_at":"2025-10-05T14:31:45.311Z","repository":{"id":57264144,"uuid":"160021085","full_name":"shaunlebron/highlight-tree-sitter","owner":"shaunlebron","description":"create syntax-highlighted code using tree-sitter","archived":true,"fork":false,"pushed_at":"2018-12-06T15:45:04.000Z","size":312,"stargazers_count":8,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-12-14T18:11:28.799Z","etag":null,"topics":["syntax-highlighter","tree-sitter"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/shaunlebron.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGES.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-12-02T06:43:16.000Z","updated_at":"2024-03-29T15:56:11.000Z","dependencies_parsed_at":"2022-08-25T02:51:43.781Z","dependency_job_id":null,"html_url":"https://github.com/shaunlebron/highlight-tree-sitter","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shaunlebron%2Fhighlight-tree-sitter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shaunlebron%2Fhighlight-tree-sitter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shaunlebron%2Fhighlight-tree-sitter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shaunlebron%2Fhighlight-tree-sitter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shaunlebron","download_url":"https://codeload.github.com/shaunlebron/highlight-tree-sitter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":235405246,"owners_count":18984868,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["syntax-highlighter","tree-sitter"],"created_at":"2024-07-31T17:01:12.703Z","updated_at":"2025-10-05T14:31:44.899Z","avatar_url":"https://github.com/shaunlebron.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# Highlight Tree Sitter\n\nA low-level API layer for [node-tree-sitter] to:\n\n- **generate HTML snippets** of syntax-highlighted source code, using [Atom's scope mappings format][scope mappings]\n- **pretty-print partial/full s-expressions** from tree-sitter syntax trees for readability/learning\n- **produce your own tree-sitter artifacts** by renaming/flattening tree-sitter nodes:\n    - (e.g. terminal color output?, or turning recognized symbols into links? whatever you want 🙂)\n\n**Background**:\n- [Atom uses tree-sitter][atom-tree-sitter] for realtime, accurate syntax parsing—as an improvement over traditional [TextMate grammars].\n- GitHub diffs syntax trees created by tree-sitter for displaying [Pull Request toc's].\n- GitHub is *not* yet using tree-sitter for syntax-highlighting hosted repos ([source](https://github.com/github/linguist/issues/4342)).\n\n**Tree-sitter is built for realtime parsing, why use it for static text?**\nTree-sitter is built for syntax-highlighting, and its promise is to have better\nerror-tolerance than other solutions.  If better grammars are developed for it,\nwe should be able to use them for highlighting text _outside_ of editors too.\n\n## Run the demo\n\nRun [demo.js](demo.js) to see the following example for highlighting JavaScript:\n\n```\nnpm install\nnode demo.js\n```\n\n## Learn!\n\n_Code below is a walkthrough of what the above demo does_\n\nSuppose we have the following JavaScript code we want to highlight:\n\n```\nfunction foo() {\n  return 1;\n}\n```\n\n**Partial Tree**: Passing it to `partialSexp` will create the partial tree seen\nbelow—not containing any actual source text, and only displaying what are\ncalled _named_ nodes, giving you an overview of the syntax tree.\n\n_NOTE: This s-expression format is what tree-sitter uses in its own test\ncases, but we provide a facility to represent it as arrays and to print it with\nproper formatting using `printSexp`, which is used in these examples)_\n\n```\n(program\n  (function\n    (identifier)\n    (formal_parameters) \n    (statement_block (return_statement (number)))))\n```\n\n**Full Tree**: Passing it to `fullSexp` will instead create a full tree, with\nsource text and whitespace with a root node `_root` capturing outer whitespace,\nand anonymous nodes `_anon` capturing what tree-sitter calls _unnamed nodes_.\n\n```\n(_root\n  \"\\n\"\n  (program\n    (function\n      (_anon \"function\")\n      \" \"\n      (identifier \"foo\")\n      (formal_parameters (_anon \"(\") (_anon \")\"))\n      \" \"\n      (statement_block\n        (_anon \"{\")\n        \"\\n  \"\n        (return_statement (_anon \"return\") \" \" (number \"1\") (_anon \";\"))\n        \"\\n\" \n        (_anon \"}\")))))\n```\n\n**Annotated Tree**: Passing the full tree to `highlightSexp` with Atom's\n[javascript grammar scopes][js-scopes] (see [scope mappings]) produces the tree\nbelow.  Each syntax node is annotated with matching class names from the scope\nmappings:\n\n```\n(_root\n  \"\\n\"\n  (program.source.js\n    (function\n      (_anon.storage.type \"function\")\n      \" \"\n      (identifier.entity.name.function \"foo\")\n      (formal_parameters\n        (_anon.punctuation.definition.parameters.begin.bracket.round \"(\")\n        (_anon.punctuation.definition.parameters.end.bracket.round \")\"))\n      \" \"\n      (statement_block\n        (_anon.punctuation.definition.function.body.begin.bracket.curly\n          \"{\")\n        \"\\n  \"\n        (return_statement\n          (_anon.keyword.control \"return\")\n          \" \"\n          (number.constant.numeric \"1\")\n          (_anon \";\"))\n        \"\\n\"\n        (_anon.punctuation.definition.function.body.end.bracket.curly\n          \"}\")))))\n```\n\n**Highlight Tree**: Since we do not need any unannotated syntax nodes, we\ncreate a new tree with only the highlighted nodes, flattening all others:\n\n```\n(_root\n  \"\\n\"\n  (program.source.js\n    (_anon.storage.type \"function\")\n    \" \"\n    (identifier.entity.name.function \"foo\")\n    (_anon.punctuation.definition.parameters.begin.bracket.round \"(\")\n    (_anon.punctuation.definition.parameters.end.bracket.round \")\")\n    \" \"\n    (_anon.punctuation.definition.function.body.begin.bracket.curly \"{\")\n    \"\\n  \"\n    (_anon.keyword.control \"return\")\n    \" \"\n    (number.constant.numeric \"1\")\n    \";\\n\"\n    (_anon.punctuation.definition.function.body.end.bracket.curly \"}\")))\n```\n\n**HTML output**: We can then directly map the highlight tree s-expressions to\nhtml span tags below:\n\n```\n\u003cspan class=\"source js\"\u003e\u003cspan class=\"storage type\"\u003efunction\u003c/span\u003e \u003cspan class=\"entity name function\"\u003efoo\u003c/span\u003e\u003cspan class=\"punctuation definition parameters begin bracket round\"\u003e(\u003c/span\u003e\u003cspan class=\"punctuation definition parameters end bracket round\"\u003e)\u003c/span\u003e \u003cspan class=\"punctuation definition function body begin bracket curly\"\u003e{\u003c/span\u003e\n  \u003cspan class=\"keyword control\"\u003ereturn\u003c/span\u003e \u003cspan class=\"constant numeric\"\u003e1\u003c/span\u003e;\n\u003cspan class=\"punctuation definition function body end bracket curly\"\u003e}\u003c/span\u003e\u003c/span\u003e\n```\n\n## API\n\nFor the following signatures, `tree` is the output of tree-sitter parser on `text`, and `sexp` is nested array of strings and arrays (s-expressions).\n\n- `partialSexp(tree) =\u003e sexp` - create partial s-expression from tree (no text or unnamed nodes)\n- `fullSexp(text, tree) =\u003e sexp` - create full s-expression from source text and tree\n- `printSexp(sexp) =\u003e str` - pretty-print an s-expression\n\nHighlighting:\n\n- `highlightSexpFromScopes(sexp, scopes) =\u003e { html, sexp }` - highlight using Atom [scope mappings]\n\n## Dev\n\nThe s-expression pretty-printer is compiled ClojureScript code.  To rebuild:\n\n```\nnpm run build\n```\n\n[tree-sitter]:https://github.com/tree-sitter/tree-sitter\n[textmate grammars]:https://macromates.com/manual/en/language_grammars\n[pull request toc's]:https://youtu.be/Jes3bD6P0To?t=1077\n[atom-tree-sitter]:https://flight-manual.atom.io/hacking-atom/sections/creating-a-grammar\n[node-tree-sitter]:https://github.com/tree-sitter/node-tree-sitter\n[scope mappings]:https://flight-manual.atom.io/hacking-atom/sections/creating-a-grammar/#syntax-highlighting\n[js-scopes]:https://github.com/atom/language-javascript/blob/v0.129.18/grammars/tree-sitter-javascript.cson#L58\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshaunlebron%2Fhighlight-tree-sitter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshaunlebron%2Fhighlight-tree-sitter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshaunlebron%2Fhighlight-tree-sitter/lists"}