Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manbearwolf/b21f89059aee69f9debeed58a753a0cf to your computer and use it in GitHub Desktop.
Save manbearwolf/b21f89059aee69f9debeed58a753a0cf to your computer and use it in GitHub Desktop.
Add line numbers to the code block generated by markdown
<?php
header('Content-type: text/css');
ob_start("compress");

  function compress($buffer) {
    /* remove comments */
      $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);

      /* remove tabs, spaces, newlines, etc. */
      $buffer = str_replace(array("\r\n", "\r", "\n", "\t", '  ', '    ', '    '), '', $buffer);

      return $buffer;
  }

  /* css files for compression */
  include('grid.css');
  include('typography.css');
  include('button.css');
  include('form.css');
  include('table.css');
  include('backgrounds.css');
  include('pagination.css');
  include('breadcrumbs.css');
  include('font.css');
  include('helpers.css');
  include('print.css');
  include('animation.css');
  include('responsive.css');

ob_end_flush();
?>
(function() {
var pre = document.getElementsByTagName('pre'),
pl = pre.length;
for (var i = 0; i < pl; i++) {
pre[i].innerHTML = '<span class="line-number"></span>' + pre[i].innerHTML + '<span class="cl"></span>';
var num = pre[i].innerHTML.split(/\n/).length;
for (var j = 0; j < (num); j++) {
var line_num = pre[i].getElementsByTagName('span')[0];
line_num.innerHTML += '<span>' + (j + 1) + '</span>';
}
}
})();
body {
padding: 100px;
font-size: 16px;
}
pre {
display: block;
margin-top: 0;
margin-bottom: 1rem;
font-size: 0.7rem;
line-height: 1.4;
white-space: pre;
overflow: auto;
background-color: #f9f9f9;
border: 1px solid #ddd;
padding: .5rem;
max-height: 800px;
font-family: monospace;
code {
color: inherit;
background-color: transparent;
padding: 0;
display: block;
}
.line-number {
display: block;
float: left;
margin: 0 1em 0 -1em;
border-right: 1px solid #ddd;
text-align: right;
span {
display: block;
padding: 0 .5em 0 1em;
color: #ccc;
}
}
.cl {
display: block;
clear: both;
}
}
@manbearwolf
Copy link
Author

      -webkit-touch-callout: none;
      -webkit-user-select: none;
      -khtml-user-select: none;
      -moz-user-select: none;
      -ms-user-select: none;
      user-select: none;

for copy button

@manbearwolf
Copy link
Author

manbearwolf commented Nov 16, 2017

Note: Not JQuery...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment