|
| 1 | +# markdown-it-named-code-blocks |
| 2 | + |
| 3 | +A [markdown-it](https://github.com/markdown-it/markdown-it#readme) plugin to create named code blocks. |
| 4 | + |
| 5 | +[](https://travis-ci.org/tsutsu3/markdown-it-named-code-blocks) |
| 6 | +[](https://www.npmjs.org/package/markdown-it-named-code-blocks) |
| 7 | +[](https://codeclimate.com/github/tsutsu3/markdown-it-named-code-blocks) |
| 8 | +[](https://github.com/tsutsu3/markdown-it-named-code-blocks/LICENSE) |
| 9 | + |
| 10 | +## 🧐 About |
| 11 | + |
| 12 | +With this pllugin you can create named code blocks like: |
| 13 | + |
| 14 | +~~~ |
| 15 | +```js:hello.js |
| 16 | +console.log("Hello World!") |
| 17 | +``` |
| 18 | +~~~ |
| 19 | + |
| 20 | +Rendered as: |
| 21 | + |
| 22 | +```html |
| 23 | +<pre class="named-fence-block"><code class="language-js">console.log("Hello World!"); |
| 24 | +</code><div class="named-fence-filename">hello.js</div></pre> |
| 25 | +``` |
| 26 | + |
| 27 | +After applying the css, it looks like this: |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | +## 🏁 Getting Started |
| 32 | + |
| 33 | +### Prerequisites |
| 34 | + |
| 35 | +- [Node.js](https://nodejs.org/) |
| 36 | + |
| 37 | +### Installing |
| 38 | + |
| 39 | +```bash |
| 40 | +npm install markdown-it-named-code-blocks |
| 41 | +``` |
| 42 | + |
| 43 | +## 🎈 Usage |
| 44 | + |
| 45 | +Use this same as a normal markdown-it plugin: |
| 46 | + |
| 47 | +```js |
| 48 | +var md = require('markdown-it'); |
| 49 | +var namedCodeBlocks = require('markdown-it-named-code-blocks'); |
| 50 | + |
| 51 | +var parser = md().use(namedCodeBlocks); |
| 52 | + |
| 53 | +var str = '```js:hello.js\nconsole.log("Hello World!);```' |
| 54 | + |
| 55 | +var result = parser.render(str); |
| 56 | +``` |
| 57 | + |
| 58 | +Apply CSS like this: |
| 59 | + |
| 60 | +```css |
| 61 | +.named-fence-block { |
| 62 | + position: relative; |
| 63 | + padding-top: 2em; |
| 64 | +} |
| 65 | + |
| 66 | +.named-fence-filename { |
| 67 | + position: absolute; |
| 68 | + top: 0; |
| 69 | + left: 0; |
| 70 | + padding: 0 4px; |
| 71 | + font-weight: bold; |
| 72 | + color: #000000; |
| 73 | + background: #c0c0c0; |
| 74 | + opacity: 0.6; |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +Rendered: |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | +If you want to enable inline CSS: |
| 83 | + |
| 84 | +```js |
| 85 | +var parser = md().use(namedCodeBlocks, {isEnableInlineCss: true}); |
| 86 | +``` |
| 87 | + |
| 88 | +```html |
| 89 | +<pre class="hljs named-fence-block" style="position: relative; padding-top: 2em"><code>console.log("Hello World!") |
| 90 | +</code><div class="mincb-name" style="position: absolute; top: 0; left: 0; padding: 0 4px; font-weight: bold; color: #000000; background: #c0c0c0; opacity: .6;">hello.js</div></pre> |
| 91 | +``` |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | +## 🎉 License |
| 96 | + |
| 97 | +Distributed under the `MIT` License. See [LICENSE]() for more information. |
0 commit comments