vuejs / vuepress

📝 Minimalistic Vue-powered static site generator
https://vuepress.vuejs.org
MIT License
22.59k stars 4.76k forks source link

Line number overwrite at code block or page level #1909

Open yongjun21 opened 5 years ago

yongjun21 commented 5 years ago

Feature request

What problem does this feature solve?

While the markdown.lineNumber config is useful, sometimes we want to overwrite specific code block or pages. For example line number is useful for demo code especially if we want to highlight specific lines. But in other case like typescript definition, we might want to hide the line numbers.

What does the proposed API look like?

Maybe use Front Matter?

How should this be implemented in your opinion?

Are you willing to work on this yourself?

ulivz commented 5 years ago

Contribution welcome!

You can pass the frontmatter setting via the 2nd parameter(env ) in the render method in markdown-loader:

https://github.com/vuejs/vuepress/blob/d858e432d4d1e910451643cd18eed15db0f03922/packages/%40vuepress/markdown-loader/index.js#L74-L78

Consuming this env at the internal lineNumbers plugin.

yongjun21 commented 5 years ago

In the meanwhile I'm using a CSS overwrite targeting language-ts specifically

roeeyn commented 5 years ago

I'd love to contribute in this feature! ✨ Can I have this assigned?

kefranabg commented 5 years ago

Sure @roeeyn

roeeyn commented 5 years ago

Hey @kefranabg 👋 I'm working on this issue, but I have some doubts. Where may I reach you? Sorry, I'm kind of newbie, but want to help 😄

roeeyn commented 5 years ago

So, my very summarized question is: what is the correct way of accessing the frontmatter from the lineNumbers plugin? Also, this will only work if the lineNumbers is set to true in the markdown config. Is that ok? Maybe we could set to true the lineNumbers by default (so the plugin adds to the chain) and only overriding it in the frontmatter. I tried following @ulivz suggestion but I was not able to reach the front matter inside the plugin. It only reaches the inside of the overridden render method.

https://github.com/vuejs/vuepress/blob/770ba72e621afdf7d758d7ffe641c73dae4e226f/packages/%40vuepress/markdown/index.js#L137-L147

Any suggestion?

FichteFoll commented 4 years ago

According to prism's line-numbers plugin, I tried adding a line-numbers class to the code block via markdown-it-attrs, but it seems that this doesn't work for code blocks in vuepress because those are processed separately (and it also uses a separate line numbers plugin).

I would love to do block-specific overrides like the following:

``` {.line-numbers}
text with
line numbers
FichteFoll commented 4 years ago

I considered disabling the built-in highlight plugin (and the others that basically depend on it) in favor of using markdown-it-prism and its plugins directly, but it doesn't appear to be disableable, unfortunately.

There is a separate issue for this already: https://github.com/vuejs/vuepress/issues/1860

Edit: Apparently, some/many prism plugins can't be used without a browser https://github.com/jGleitz/markdown-it-prism/issues/1.

trapcodeio commented 3 years ago

In the meanwhile I'm using a CSS overwrite targeting language-ts specifically

This actually helped and I recommend it. E.g I don't need line numbers in shell. Using .styl

.language-shell.line-numbers-mode
  .line-numbers-wrapper
    display none