eslint-doc-generator
Version:
Automatic documentation generator for ESLint plugins and rules.
28 lines (27 loc) • 984 B
JavaScript
/** Uppercase the first character of a string, leaving the rest unchanged. */
function upperFirst(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
export function toSentenceCase(str) {
// Only uppercase a leading word character, leaving the rest of the string as-is.
return str.replace(/^\w/u, (char) => char.toUpperCase());
}
export function addTrailingPeriod(str) {
return str.replace(/\.?$/u, '.');
}
export function removeTrailingPeriod(str) {
return str.replace(/\.$/u, '');
}
/**
* Example: FOO => Foo, foo => Foo
*/
export function capitalizeOnlyFirstLetter(str) {
return upperFirst(str.toLowerCase());
}
function sanitizeMarkdownTableCell(text) {
// Handle CRLF line endings too since cell text can come from rule metadata.
return text.replaceAll('|', String.raw `\|`).replaceAll(/\r?\n/gu, '<br/>');
}
export function sanitizeMarkdownTable(text) {
return text.map((row) => row.map((col) => sanitizeMarkdownTableCell(col)));
}