Skip to content

Commit

Permalink
issues/6-bug: dangling comment block
Browse files Browse the repository at this point in the history
  • Loading branch information
Seven-Y-Q-Guo committed Feb 15, 2024
1 parent e10bac2 commit 49ea34a
Showing 1 changed file with 35 additions and 4 deletions.
39 changes: 35 additions & 4 deletions packages/shaku-code-annotate-shiki/src/codeToShakuHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ interface CodeToShakuHtmlOptions {
markdownToHtmlAndSanitize?: (md: string) => string;
}

export let codeToShakuHtml = function (
export let codeToShakuHtml = function(
this: ShakuHighlighter,
{
code,
Expand Down Expand Up @@ -64,7 +64,7 @@ export let codeToShakuHtml = function (
}
}

const lines = highlighter.codeToThemedTokens(code, lang);
const lines = transformLines(highlighter.codeToThemedTokens(code, lang));
const foregroundColor = highlighter.getForegroundColor();
const backgroundColor = highlighter.getBackgroundColor();

Expand Down Expand Up @@ -329,8 +329,8 @@ export let codeToShakuHtml = function (
diff === "+"
? " diff diff-insert"
: diff === "-"
? " diff diff-delete"
: "";
? " diff diff-delete"
: "";

const prefix = `<div class="line${highlightClass}${dimClass}${diffClass}">`;
html += prefix;
Expand All @@ -357,6 +357,37 @@ export let codeToShakuHtml = function (
};
};

function transformLines(lines: IThemedToken[][]) {
let isMultiLineCommentsStart: Boolean = false;

return lines
.map((line) => {
if (line[0]) {
if (line[0].content.indexOf("/*") > -1) {
isMultiLineCommentsStart = true;
line[0].content = line[0].content.replace("/*", "//");
}

if (line[0].content.indexOf("*/") > -1) {
isMultiLineCommentsStart = false;
line[0].content = line[0].content.replace("*/", "@removed");
} else {
if (isMultiLineCommentsStart) {
if (
line[0].content.trim() &&
line[0].content.trim().slice(0, 2) !== "//"
) {
line[0].content = "//" + line[0].content;
}
}
}
}

return line;
})
.filter((line) => line[0]?.content !== "@removed");
}

/**
* different kinds of comments have different interpretations
* Below are some common examples, these are not exhaustive
Expand Down

0 comments on commit 49ea34a

Please sign in to comment.