Skip to content

Instantly share code, notes, and snippets.

@salif
Created November 14, 2022 18:26
Show Gist options
  • Save salif/dece6e7499f485f9d81c1e32182c0353 to your computer and use it in GitHub Desktop.
Save salif/dece6e7499f485f9d81c1e32182c0353 to your computer and use it in GitHub Desktop.
Edit Markdown Table

Example

edit_markdown_table(`
|key|value|
|:-|-:|
|a|1|
|b|2|
|c|3|
`, [1, 0])

Before

key value
a 1
b 2
c 3

After

value key
1 a
2 b
3 c
function edit_markdown_table(a, w) {
const b = a.trim().split('\n')
const o = []
for (let i = 0; i < b.length; i++) {
const c = b[i].split('|')
c.shift()
c.pop()
const d = [""]
const r = d.concat(w.map(e => c[e]))
r.push("")
o.push(r.join('|'))
}
return o.join('\n')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment