Skip to content
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

Feature Request: Support HTML-style comments for cut functionality in Twoslash #64

Open
Fro-Q opened this issue Feb 6, 2025 · 2 comments

Comments

@Fro-Q
Copy link

Fro-Q commented Feb 6, 2025

Problem

I'm using Twoslash in a VitePress project and encountered an issue where I need to display an error for a named import in a Vue SFC. Specifically, I have a TypeScript file posts.data.ts that only has a default export, but I'm attempting to import it in a Vue SFC using a named import, which should trigger an error.

Here's the relevant part of my markdown content:

<script>
// ---cut-start---
// @filename: ../src/posts.data.ts
import { createContentLoader } from "vitepress";

export default createContentLoader("posts/*.md", {
// ...
});
// ---cut-end---
</script>

<script setup lang="ts">
// @errors: 2614
import { data as posts } from "../src/posts.data";
// ...
</script>

<template>
  <!-- ... -->
</template>

This setup correctly displays the error I want. However, I noticed that the cut functionality only works with JavaScript-style comments (//), which means it cannot hide the <script> and </script> tags (lines 1 and 10).

Request

Would it be possible to extend the cut functionality to support HTML-style comments? For example, something like this:

<!-- ---cut-start--- -->
<script>
// @filename: ../src/posts.data.ts
import { createContentLoader } from "vitepress";

export default createContentLoader("posts/*.md", {
// ...
});
</script>
<!-- ---cut-end--- -->

<script setup lang="ts">
// @errors: 2614
import { data as posts } from "../src/posts.data";
// ...
</script>

<template>
  <!-- ... -->
</template>

I can confirm that the cut functionality in the example above does not currently work. For context, I'm using [email protected] and [email protected].

@antfu
Copy link
Member

antfu commented Feb 7, 2025

Sure, PR welcome :)

@Fro-Q
Copy link
Author

Fro-Q commented Feb 7, 2025

After checking the source code, I noticed that twoslash-vue does not process the entire Vue SFC but only focuses on the <script> block. This is why // ---cut--- does not work outside of <script>, even when the regex matches. I agree that this design should remain unchanged.

Instead, I will explore whether Shiki can support a transformer to hide specific lines. See you there ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants