-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
base: main
Are you sure you want to change the base?
Added copy button #2279
Changes from 1 commit
d8ac257
c395d86
2fd2bb9
588863d
e99146d
bd89231
963cd24
f88ee19
584ff3a
bc6e02d
2dd99dd
d431196
358e1a9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
const codeTagRegex = /\[!code\s+([^\]]+)\]/; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We check every comment. If there is a Shiki match:
|
||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
There was a problem hiding this comment.
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.