Skip to content

Added copy button #2279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
49 changes: 49 additions & 0 deletions src/components/code-example.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,55 @@ export function css(strings: TemplateStringsArray, ...args: any[]) {
return { lang: "css", code: dedent(strings, ...args) };
}

export function unshiki(code: string): string {
const lines = code.split("\n");
const result: string[] = [];
let skip = 0;

const commentRegex = /\/\*.*?\*\/|\/\/.*|<!--.*?-->|#.*/g;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm trying to filter out comments found in all kinds of languages as briefly as possible. Since this is client-side code that runs only occasionally, I believe it's fine to handle it with regex.

const codeTagRegex = /\[!code\s+([^\]]+)\]/;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also used regex to detect Shiki comments.


for (let i = 0; i < lines.length; i++) {
// skip lines if a remove directive is active
if (skip > 0) {
skip--;
continue;
}

let line = lines[i];
const comments = [...line.matchAll(commentRegex)];

let removed = false;

// process comments to detect [!code ...] directives
for (const c of comments) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We check every comment. If there is a Shiki match:

  • we handle the -- case specially in diffs (the skip handling is necessary for this reason)
  • otherwise, we just remove the comment from the line

const match = c[0].match(codeTagRegex);
if (match) {
// check if directive to remove next N lines
const spec = match[1];
const removeMatch = spec.match(/^--:(\d+)$/);
if (removeMatch) {
// set lines to skip
skip = parseInt(removeMatch[1], 10) - 1;
// current line removed (important if the line is not just a comment but also valid code)
removed = true;
break;
}

// remove comment if it's not a remove directive
line = line.slice(0, c.index) + line.slice(c.index! + c[0].length);
}
}

// add line if not removed and line is not empty or has no comments
if (!removed && (comments.length === 0 || line.trim() !== "")) {
result.push(line);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After that, we add to the result those lines that don't need to be removed (special case) and either had no comments or had comments that were removed, leaving the line empty.

}
}

return result.join('\n').trim();
}

export async function CodeExample({
example,
filename,
Expand Down