{"id":13623461,"url":"https://github.com/ShaunLawrie/PwshSyntaxHighlight","last_synced_at":"2025-04-15T14:33:05.366Z","repository":{"id":167484305,"uuid":"643088223","full_name":"ShaunLawrie/PwshSyntaxHighlight","owner":"ShaunLawrie","description":"🌈 Render PowerShell in the Terminal with Syntax Highlighting","archived":false,"fork":false,"pushed_at":"2023-05-25T10:57:49.000Z","size":133,"stargazers_count":39,"open_issues_count":1,"forks_count":5,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-04-06T06:31:26.232Z","etag":null,"topics":["powershell","powershell-module","psgallery"],"latest_commit_sha":null,"homepage":"https://shaunlawrie.com/","language":"PowerShell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ShaunLawrie.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-05-20T04:23:20.000Z","updated_at":"2025-04-04T03:32:04.000Z","dependencies_parsed_at":"2023-05-26T23:00:42.719Z","dependency_job_id":null,"html_url":"https://github.com/ShaunLawrie/PwshSyntaxHighlight","commit_stats":null,"previous_names":["shaunlawrie/pwshsyntaxhighlight"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ShaunLawrie%2FPwshSyntaxHighlight","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ShaunLawrie%2FPwshSyntaxHighlight/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ShaunLawrie%2FPwshSyntaxHighlight/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ShaunLawrie%2FPwshSyntaxHighlight/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ShaunLawrie","download_url":"https://codeload.github.com/ShaunLawrie/PwshSyntaxHighlight/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249089077,"owners_count":21210913,"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":["powershell","powershell-module","psgallery"],"created_at":"2024-08-01T21:01:32.062Z","updated_at":"2025-04-15T14:33:05.085Z","avatar_url":"https://github.com/ShaunLawrie.png","language":"PowerShell","funding_links":[],"categories":["PowerShell"],"sub_categories":[],"readme":"# 🌈 PwshSyntaxHighlight\n\n[![PowerShell Gallery](https://img.shields.io/powershellgallery/v/PwshSyntaxHighlight)](https://www.powershellgallery.com/packages/PwshSyntaxHighlight)\n[![PowerShell Gallery](https://img.shields.io/powershellgallery/dt/PwshSyntaxHighlight)](https://www.powershellgallery.com/packages/PwshSyntaxHighlight)\n[![GitHub license](https://img.shields.io/github/license/ShaunLawrie/PwshSyntaxHighlight)](https://github.com/ShaunLawrie/PwshSyntaxHighlight/blob/main/LICENSE)\n\nPwshSyntaxHighlight is a simple syntax highlighter for rendering PowerShell code blocks inside the terminal. This is a standalone version of the codeblock renderer I built for [PowerShell AI](https://github.com/dfinke/PowerShellAI).\n\n![image](https://github.com/ShaunLawrie/PwshSyntaxHighlight/assets/13159458/9b3f4a2d-79c0-4446-aeea-6553b830a819)\n\n# Install\n\n```pwsh\nInstall-Module PwshSyntaxHighlight -Scope CurrentUser\n```\n\n# Usage\n\n## 1. Generate Sample Code\n\nCreate some sample code to use in the examples below using a multi-line string variable like this.\n```pwsh\n$sampleCode = @'\nfunction Test-ThisThingOut {\n    param (\n        [string] $Parameter\n    )\n    $message = \"$Parameter is the parameter\"\n    Write-Host \"Hello PwshSyntaxHighlight! $message\"\n}\n'@\n```\n\n## 2. Render Code Blocks\n\nPass the code as the `-Text` parameter to render a code block, use `-SyntaxHighlight` to enable highlighting and `-ShowLineNumbers` to show a gutter down the left containing the code line numbers.\n```pwsh\nWrite-CodeBlock -Text $sampleCode -SyntaxHighlight -ShowLineNumbers\n```\n![Example of syntax highlighting](/PwshSyntaxHighlight/private/PwshSyntaxHighlightExample1.png)\n\n## 3. Use Themes\n\nThere are only two basic themes available at the moment, GitHub (default) and Matrix, if you're interested you could open a PR for more.\n```pwsh\nWrite-CodeBlock -Text $sampleCode -SyntaxHighlight -ShowLineNumbers -Theme Matrix\n```\n![Example of syntax highlighting](/PwshSyntaxHighlight/private/PwshSyntaxHighlightExample4.png)\n\n## 4. Highlight Specific Lines\n\n```pwsh\n# Provide a list of lines to highlight to draw attention to them with -HighlightLines\nWrite-CodeBlock $sampleCode -HighlightLines 2, 3, 4 -ShowLineNumbers\n```\n![Example of syntax highlighting](/PwshSyntaxHighlight/private/PwshSyntaxHighlightExample2.png)\n\n## 5. Highlight Extents\n\nBy passing a collection of Extents into `Write-CodeBlock` you can have the extents highlighted in the rendered code. This is how errors are highlighted in the [PowerShell AI Function Builder demo on YouTube](https://youtu.be/MbHTrVdTJXE).  \nParsing PowerShell code with the built-in Abstract Syntax Tree is too much for me to document here but you can check out the awesome details available at [powershell.one/powershell-internals/parsing-and-tokenization/abstract-syntax-tree](https://powershell.one/powershell-internals/parsing-and-tokenization/abstract-syntax-tree) if you're interested.  \n\n```pwsh\n# Create a script block from the sample code\n$scriptBlock = [ScriptBlock]::Create($sampleCode)\n\n# Get a list of items from the abstract syntax tree that are double quoted strings\n$astItems = $scriptBlock.Ast.FindAll({$args[0].StringConstantType -eq \"DoubleQuoted\"}, $true)\n\n# Get the extents for all of those double quoted strings\n$extents = $astItems | Select-Object -ExpandProperty Extent\n\n$sampleCode | Write-CodeBlock -HighlightExtents $extents -ShowLineNumbers\n```\n![Example of syntax highlighting](/PwshSyntaxHighlight/private/PwshSyntaxHighlightExample3.png)\n\n## 6. Export Screenshots as PNG (Windows Only)\n\n[@I_Am_Jakoby](https://twitter.com/I_Am_Jakoby/status/1660673439705837570) added the ability to take screenshots when this is run from a Windows based terminal 🤙  \nUse `-ClearHost` and `-ScreenShot` to clear the host, and save an image of the terminal window.\n\n```pwsh\nWrite-CodeBlock $sampleCode -SyntaxHighlight -ShowLineNumbers -ClearHost -ScreenShot\n```\n![Example of screenshot](/PwshSyntaxHighlight/private/PwshSyntaxHighlightExample5.png)\n\nThe screenshot will include the window border and look a bit like this:  \n![Example of screenshot output](/PwshSyntaxHighlight/private/PwshSyntaxHighlightExample6.png)\n\n## 7. Export Screenshots as HTML (Windows Only)\n\nAnother [@I_Am_Jakoby](https://twitter.com/I_Am_Jakoby) special 🙏  \nUse `-Html` to save the code block as an html document.  \n\n```pwsh\nWrite-CodeBlock $sampleCode -SyntaxHighlight -ShowLineNumbers -Html\n```\n![Example of html command](/PwshSyntaxHighlight/private/PwshSyntaxHighlightExample7.png)\n\nThe command will save an HTML file you can open in your browser:  \n![Example of html output](/PwshSyntaxHighlight/private/PwshSyntaxHighlightExample8.png)\n*The colors don't match what you see in the terminal which will be corrected with https://github.com/ShaunLawrie/PwshSyntaxHighlight/issues/2*","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FShaunLawrie%2FPwshSyntaxHighlight","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FShaunLawrie%2FPwshSyntaxHighlight","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FShaunLawrie%2FPwshSyntaxHighlight/lists"}