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

feat: add support collapsed-lines for code highlighters #246

Merged
merged 9 commits into from
Sep 16, 2024

Conversation

pengzhanbo
Copy link
Member

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Provide a description in this PR that addresses what the PR is solving. If this PR is going to solve an existing issue, please reference the issue (e.g. close #123).

What is the purpose of this pull request?

  • Bug fix
  • New feature
  • Other

Description

Screenshots

Before

After

@coveralls
Copy link

coveralls commented Sep 13, 2024

Pull Request Test Coverage Report for Build 10871006435

Details

  • 1 of 24 (4.17%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.3%) to 57.817%

Changes Missing Coverage Covered Lines Changed/Added Lines %
plugins/markdown/plugin-prismjs/src/node/prepareConfigFile.ts 0 8 0.0%
plugins/markdown/plugin-prismjs/src/node/prismjsPlugin.ts 1 16 6.25%
Totals Coverage Status
Change from base Build 10862365305: 0.3%
Covered Lines: 1122
Relevant Lines: 1750

💛 - Coveralls

@pengzhanbo pengzhanbo marked this pull request as ready for review September 14, 2024 10:40
@@ -78,5 +80,22 @@ export const prepareConfigFile = (
)
}

return app.writeTemp('prismjs/config.js', imports.join('\n'))
imports.push(
`import "${getRealPath('@vuepress/highlighter-helper/styles/collapsed-lines.css', url)}"`,
Copy link
Member

Choose a reason for hiding this comment

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

I think we should only import these when the feature is enabled?

@@ -29,6 +33,7 @@ export const prismjsPlugin = ({
md.use<PreWrapperOptions>(preWrapperPlugin, { preWrapper })
if (preWrapper) {
md.use(lineNumbersPlugin, { lineNumbers, removeLastLine: true })
md.use(collapsedLinesPlugin, { collapsedLines, removeLastLine: true })
Copy link
Member

Choose a reason for hiding this comment

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

Why this plugin should be used anyway?

removeLastLine: true,
})
md.use(lineNumbersPlugin, { lineNumbers, removeLastLine: true })
md.use(collapsedLinesPlugin, { collapsedLines, removeLastLine: true })
Copy link
Member

Choose a reason for hiding this comment

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

same

@@ -64,5 +66,22 @@ export const prepareConfigFile = (
)
}

return app.writeTemp('shiki/config.js', imports.join('\n'))
imports.push(
Copy link
Member

Choose a reason for hiding this comment

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

same

if (preWrapper) {
md.use<MarkdownItLineNumbersOptions>(lineNumbersPlugin, { lineNumbers })
md.use(lineNumbersPlugin, { lineNumbers })
md.use(collapsedLinesPlugin, { collapsedLines })
Copy link
Member

Choose a reason for hiding this comment

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

same

selector = 'div[class*="language-"].has-collapsed-lines > .collapsed-lines',
}: { selector?: string } = {}): void => {
if (__VUEPRESS_SSR__) return
window.addEventListener('click', (e) => {
Copy link
Member

Choose a reason for hiding this comment

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

I would prefer useEventListener, I discourage to see __VUEPRESS_SSR__ as a judgement to visit window outside onMounted

Comment on lines +88 to +92
@property --vp-collapsed-lines-bg {
inherits: false;
initial-value: #fff;
syntax: '<color>';
}
Copy link
Member

Choose a reason for hiding this comment

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

Is this needed?

https://developer.mozilla.org/zh-CN/docs/Web/CSS/@property From the table, firefox only supports it from 128. I am not sure if there will be errors under lower version of firfox.

Copy link
Member Author

Choose a reason for hiding this comment

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

Don't worry, in lower versions it will just be recognized as a regular CSS variable, and after the downgrade, the corresponding background gradient transition animation simply won't take effect.

@@ -23,7 +23,7 @@ div[class*='language-'] {
border-right: 1px solid var(--code-c-highlight-bg, var(--code-c-text));
border-radius: var(--code-border-radius) 0 0 var(--code-border-radius);

transition: border var(--t-color);
transition: border var(--vp-t-color);
Copy link
Member

Choose a reason for hiding this comment

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

nice catch, could this be in a sperate PR?

Copy link
Member Author

@pengzhanbo pengzhanbo Sep 14, 2024

Choose a reason for hiding this comment

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

I see that it was fixed in another commit.

Copy link
Member

@Mister-Hope Mister-Hope left a comment

Choose a reason for hiding this comment

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

Some documentation improvements are suggested


- `number`: collapse the code block starting from line `number`, for example, `12` means collapsing the code block starting from line 12.
- `true`: Equivalent to `15`, collapsing the code block starting from line 15.
- `false`: Disable code block collapsed, but `:collapsed-lines` can still be added to collapse individual code blocks.
Copy link
Member

Choose a reason for hiding this comment

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

I think the description can be changed to:

Add support for code block collapsing, but disable it globally

Also I would prefer the word code block collapsing instead of code block collapsed.

- `false`: Disable code block collapsed, but `:collapsed-lines` can still be added to collapse individual code blocks.
- `'disabled'`: Completely disable code block collapsed, `:collapsed-lines` will not take effect.

You can add the `:collapsed-lines` / `:no-collapsed-lines` marker to the code block to override the settings in the configuration item. You can also add `=` after `:collapsed-lines` to customize the starting collapsed line number, for example, `:collapsed-lines=12` means collapsing the code block starting from line 12.
Copy link
Member

Choose a reason for hiding this comment

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

to customize the starting line number being collapsed

- `false`: Disable code block collapsed, but `:collapsed-lines` can still be added to collapse individual code blocks.
- `'disabled'`: Completely disable code block collapsed, `:collapsed-lines` will not take effect.

You can add the `:collapsed-lines` / `:no-collapsed-lines` marker to the code block to override the settings in the configuration item. You can also add `=` after `:collapsed-lines` to customize the starting collapsed line number, for example, `:collapsed-lines=12` means collapsing the code block starting from line 12.
Copy link
Member

Choose a reason for hiding this comment

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

To override global settings, you can add the xxx marker to code blocks.


- Default: `'disabled'`

- Details: Whether to enable code block collapsing.
Copy link
Member

Choose a reason for hiding this comment

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

I think this could be changed to Default behavior of code block collapsing

And the description of the first two settings could be added with the words by default

@Mister-Hope Mister-Hope merged commit c0f70e4 into main Sep 16, 2024
29 checks passed
@Mister-Hope Mister-Hope deleted the collapsed-lines branch September 16, 2024 02:17
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

Successfully merging this pull request may close these issues.

3 participants