Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## Script to convert Markdown to Plain text, using an Espanso trigger:
- #!/usr/bin/env pwsh
- param (
- [string]$markdownText
- )
- # Function to convert Markdown to plain text
- function Convert-MarkdownToText {
- param (
- [string]$markdown
- )
- # Split the input into lines to process each line individually
- $lines = $markdown -split "`n"
- $inCodeBlock = $false
- $plainTextLines = $lines | ForEach-Object {
- # Toggle code block flag when encountering ``` lines
- if ($_ -match '^\s*```') {
- $inCodeBlock = -not $inCodeBlock
- # Skip the ``` lines themselves
- return
- }
- # If inside a code block, capture content directly without modification
- if ($inCodeBlock) {
- return $_
- }
- # Process each line for Markdown syntax if not in a code block
- $_ -replace '^\s*#{1,6}\s*(.+)$', '$1' | # Remove headers
- ForEach-Object { $_ -replace '\*\*([^\*]+)\*\*', '$1' } | # Bold
- ForEach-Object { $_ -replace '\*([^\*]+)\*', '$1' } | # Italic
- ForEach-Object { $_ -replace '__(.+?)__', '$1' } | # Bold underline
- ForEach-Object { $_ -replace '_(.+?)_', '$1' } | # Italic underline
- ForEach-Object { $_ -replace '\[([^\]]+)\]\([^\)]+\)', '$1' } | # Links
- ForEach-Object { $_ -replace '!\[([^\]]*)\]\([^\)]+\)', '$1' } | # Images
- ForEach-Object { $_ -replace '``(.+?)``', '$1' } | # Inline code
- ForEach-Object { $_ -replace '`(.+?)`', '$1' } | # Inline code
- ForEach-Object { $_ -replace '^\s*[-\*\+]\s+', '' } | # Unordered list markers
- ForEach-Object { $_ -replace '^\s*\d+\.\s+', '' } | # Ordered list markers
- ForEach-Object { $_ -replace '^\s*>\s+', '' } | # Blockquotes
- ForEach-Object { $_ -replace '\[\[([^\]]+)\]\]', '$1' } # Convert [[text]] to text
- }
- # Join the processed lines back into a single string
- $plainText = ($plainTextLines -join "`n").Trim()
- return $plainText
- }
- # Convert markdownText from input
- $plainText = Convert-MarkdownToText -markdown $markdownText
- Write-Output $plainText
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement