@redocly/theme
Version:
Shared UI components lib
17 lines (14 loc) • 574 B
text/typescript
const NEW_LINE_EXP = /\n(?!$)/g;
/**
* @param highlightedCode - A string with new line breaks; the line numbers will be added before each line.
* @param start - The number to start the line numbering from; default is 1
* @returns A string with line numbers added before each line
*/
export function addLineNumbers(highlightedCode: string, start = 1): string {
const codeLines = highlightedCode.split(NEW_LINE_EXP);
return codeLines
.map((line, i) => {
return `<span class='line' data-line-number='${start + i}'>${line}</span>`;
})
.join('\n');
}