Skip to content

Instantly share code, notes, and snippets.

@also
Created February 2, 2017 01:24
Show Gist options
  • Save also/11a528e5cb95c89642023fe0a6d79f82 to your computer and use it in GitHub Desktop.
Save also/11a528e5cb95c89642023fe0a6d79f82 to your computer and use it in GitHub Desktop.
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