Skip to content

Instantly share code, notes, and snippets.

@pohatu
Last active August 29, 2015 13:59
Show Gist options
  • Save pohatu/10701938 to your computer and use it in GitHub Desktop.
Save pohatu/10701938 to your computer and use it in GitHub Desktop.
ConvertTo-MarkDownTable
function ConvertTo-MarkDownTable()
{
$in = [windows.clipboard]::GetText()
$lines = $in.split("`n")
$out=@()
$out += $lines[0] -replace "`r",'' -replace "^","|" -replace ",","|" -replace "$","|`n"
$out += $lines[0] -replace "`r",'' -replace "\w+","---" -replace "^","|" -replace ",","|" -replace "$","|`n"
$out += $lines[1..($lines.Count-1)] -replace "`r", '' -replace "^", "|" -replace ",","|" -replace "$", "|`n"
[windows.clipboard]::SetText($out -join "")
}
## input is a comma-seperated multi-line segment
## ex:
## foo,bar,buzz,bazz,biff
## 1,2,3,4,5
## 6,7,8,9,10
##
##
## output is markdown-style table
##
## example:
## |foo|bar|buzz|bazz|biff|
## |---|---|---|---|---|
## |1|2|3|4|5|
## |6|7|8|9|10|
## doesn't care about right-aligned or centered, just does default.
## Doesn't care about pretty.
@pohatu
Copy link
Author

pohatu commented Apr 15, 2014

The following:

foo,bar,buzz,bazz,biff
1,2,3,4,5
6,7,8,9,10

becomes:

|foo|bar|buzz|bazz|biff|
|---|---|---|---|---|
|1|2|3|4|5|
|6|7|8|9|10|

which renders as:

foo bar buzz bazz biff
1 2 3 4 5
6 7 8 9 10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment