Created
February 2, 2017 01:24
-
-
Save also/11a528e5cb95c89642023fe0a6d79f82 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const lines = str.trim().split('\n'); | |
const groups = []; | |
let input; | |
lines.forEach((line) => { | |
if (input) { | |
input.push(line); | |
if (line[line.length - 1] !== '\\') { | |
input = null; | |
} | |
} else if (line.startsWith('$ ')) { | |
const lines = [line.substr(2)]; | |
groups.push({type: 'input', lines}); | |
if (line[line.length - 1] === '\\') { | |
input = lines; | |
} | |
} else { | |
const type = line[0] === '#' ? 'comment': 'output'; | |
groups.push({type, lines: [line]}); | |
} | |
}); | |
const result = groups.map(({type, lines}) => { | |
if (type === 'comment') { | |
return `<span class="hljs-section">${escapeHtml(lines.join('\n'))}</span>`; | |
} else if (type === 'input') { | |
return `<strong>$ </strong><em>${escapeHtml(lines.join('\n'))}</span>`; | |
} else { | |
return `<span>${escapeHtml(lines.join('\n'))}</span>`; | |
} | |
}); | |
return result.join('\n'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment