Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function replaceHashComment(line: string): string {
- const makeInlineComment = (comment: string) => {
- const value = comment?.trim();
- if (!value) {
- return '<##>';
- }
- return `<# ${value} #>`;
- };
- const parts = line.split('#');
- if (parts.length === 1) { // No hash
- return line;
- } else if (parts.length === 2) {
- const code = parts[0];
- return code + makeInlineComment(parts[1]);
- } else {
- const firstDashIndex = parts[0].length;
- if (firstDashIndex === 0) { // Starts with dash
- return makeInlineComment(parts[1]);
- }
- // Do not inline if it's inline comment
- const characterBeforeDash = line[firstDashIndex - 1];
- if (characterBeforeDash === '<') {
- for (let i = 1; i < parts.length; i++) {
- const characterAfterNextDash = parts[i][0];
- if (characterAfterNextDash === '>') {
- return line;
- }
- }
- }
- const textAfterFirstDash = line.substring(firstDashIndex + 1);
- return parts[0] + makeInlineComment(textAfterFirstDash);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment