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

Custom Containers with more options #536

Closed
PanJiaChen opened this issue Jun 1, 2018 · 11 comments
Closed

Custom Containers with more options #536

PanJiaChen opened this issue Jun 1, 2018 · 11 comments

Comments

@PanJiaChen
Copy link
Contributor

Feature request

First

return `<div class="${klass} custom-block"><p class="custom-block-title">${info || defaultTitle}</p>\n`

Sometimes l just want Custom Containers without title.

Second

And is it possible that I can customize its class? So I can simply create a custom container with custom css.

image

@meteorlxy
Copy link
Member

How about override the default style in override.styl?

@PanJiaChen
Copy link
Contributor Author

PanJiaChen commented Jun 1, 2018

I don't want to override the style, I also want to continue to use the default style.
And overriding styles means that I can only use three types of container, no more types could l add.

@ulivz
Copy link
Member

ulivz commented Jun 1, 2018

You can create your custom container by extending the md instance via config/#markdown-config.

Closing as this can be done in userland and we don't plan to add built-in support for it.

@ulivz ulivz closed this as completed Jun 1, 2018
@GrayedFox
Copy link

GrayedFox commented Nov 21, 2018

This is nearly a direct copy paste of the code used by Vuepress to generate custom containers (some minor changes so that our internal linting rules and style checks pass):

const container = require('markdown-it-container')

function createContainer(type, defaultTitle) {
  return [container, type, {
    render(tokens, idx) {
      const token = tokens[idx]
      const info = token.info.trim().slice(type.length).trim()
      const title = info || defaultTitle
      if (token.nesting === 1) {
        return `<div class="${type} custom-block"><p class="custom-block-title">${title}</p>\n`
      } else {
        return `</div>\n`
      }
    }
  }]
}

// md needs to be an instance of markdown-it (in our case, the one used by mavonEditor)
module.exports = (md) => {
  md
    .use(...createContainer('tip', 'TIP') )
    .use(...createContainer('warning', 'WARNING') )
    .use(...createContainer('danger', 'WARNING') )
}

To create your own custom container that you can style how you like, just create another one like: .use(...createContainer('custom', 'defaultTitle'))

Now you can style is with .custom margin: 1em etc.

Update: just noticed Vue has updated custom containers to explicitly escape Vue syntax - follow the link and see if you also need that for your own custom containers.

@lunaceee
Copy link

@GrayedFox I can't seem to open the link you attached anymore, could you please explain a bit more about how you implemented this in your code? Where shall I add the function? I'm new to VuePress, thanks!

@shigma
Copy link
Collaborator

shigma commented Mar 14, 2019

@lunaceee For now you can try @vuepress/plugin-container.

@lunaceee
Copy link

Thanks @shigma Didn't realize it's part of the 1.x feature. :)

@lunaceee
Copy link

@shigma I installed the plugin, got the type working but the container doesn't seem to have any style. What's the expected behavior?

@shigma
Copy link
Collaborator

shigma commented Mar 14, 2019

@lunaceee

Our default theme has provided styles for tip, warnging and danger. You can use them.

If you want to define your own custom blocks, you should provide styles by yourself.

See: https://github.com/vuejs/vuepress/blob/master/packages/%40vuepress/theme-default/styles/custom-blocks.styl

@germanbobadilla
Copy link

germanbobadilla commented Mar 22, 2020

I'm stuck here. I've installed the plugin, used it in config, but still. It doesn't work. I don't know what I'm doing wrong. Look:

['@vuepress-plugin-container', {
          type: 'warning',          
          defaultTitle: 'WARNING',
        }],
        ['@vuepress-plugin-container', {
          type: 'tip',          
          defaultTitle: 'TIP',
        }],
        ['@vuepress-plugin-container', {
          type: 'theorem',
          before: tip => `<div class="theorem"><p class="title">${tip}</p>`,
          after: '</div>',
        }],

@haoranpb
Copy link
Contributor

Hi @germanbobadilla ,You don't need to manually install nor config vuepress-plugin-container if you are using the default theme. Can you share your repo or provide a minimal reproduction?

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

8 participants