Skip to content

Instantly share code, notes, and snippets.

@smeech
Created January 31, 2025 16:31
Show Gist options
  • Select an option

  • Save smeech/e35572aeea64ff2cced01ecf5a4c255f to your computer and use it in GitHub Desktop.

Select an option

Save smeech/e35572aeea64ff2cced01ecf5a4c255f to your computer and use it in GitHub Desktop.
[Convert markdown] Script to convert Markdown to Plain text, using an Espanso trigger. #espanso #powershell #python
## Espanso trigger to accompany ConvertMarkdown.ps1
- trigger: :conv
replace: '{{output}}'
vars:
- name: clipb
type: clipboard
- name: output
type: script
params:
args:
- pwsh
- /home/stephen/.config/espanso/scripts/ConvertMarkdown.ps1
- -markdownText
- "{{clipb}}"
## Neater inline solution using Python libraries instead,
## to convert Markdown→HTML→Plain text:
- trigger: :conv
replace: '{{output}}'
vars:
- name: clipb
type: clipboard
- name: output
type: script
params:
args:
- python
- -c
- |
from bs4 import BeautifulSoup
from markdown import markdown
import re
args = """{{clipb}}"""
html = markdown(args)
html = re.sub(r'<pre>(.*?)</pre>', ' ', html)
html = re.sub(r'<code>(.*?)</code>', ' ', html)
text = ''.join(BeautifulSoup(html, "html.parser").findAll(text=True))
print(text)
## 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment