Skip to content

Commit

Permalink
♻️ simplify comment parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
ebullient committed Aug 12, 2024
1 parent eb7c5be commit 9c28d77
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
7 changes: 7 additions & 0 deletions src/obsidian/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ export class CommentParser {
return this.parseRegex.test(line);
}

replace(line: string, comment: Comment): string {
return line.replace(
this.readCommentRegex,
this.commentToString(comment),
);
}

private readCommentStringFromLine(line: string): string {
return this.readCommentRegex.exec(line)?.[0] ?? '';
}
Expand Down
12 changes: 3 additions & 9 deletions src/obsidian/processors/commentProcessor.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
import { CommentParser } from 'src/obsidian/comment';

export class CommentProcessor {
private readCommentRegex = /<!--.*-->/;

private parser: CommentParser = new CommentParser();

process(markdown: string) {
return markdown
.split('\n')
.map(line => {
if (this.parser.lineHasComment(line)) {
return line.replace(
this.readCommentRegex,
this.parser.commentToString(
this.parser.parseLine(line),
),
);
const comment = this.parser.parseLine(line);
if (comment) {
return this.parser.replace(line, comment);
} else {
return line;
}
Expand Down

0 comments on commit 9c28d77

Please sign in to comment.