webpack.markdown-loader.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /**
  2. * React Static Boilerplate
  3. * https://github.com/koistya/react-static-boilerplate
  4. *
  5. * Copyright © 2015-2016 Konstantin Tarkus (@koistya)
  6. *
  7. * This source code is licensed under the MIT license found in the
  8. * LICENSE.txt file in the root directory of this source tree.
  9. */
  10. const MarkdownIt = require('markdown-it');
  11. const hljs = require('highlight.js');
  12. const fm = require('front-matter');
  13. module.exports = function markdown(source) {
  14. this.cacheable();
  15. const md = new MarkdownIt({
  16. html: true,
  17. linkify: true,
  18. highlight: (str, lang) => {
  19. if (lang && hljs.getLanguage(lang)) {
  20. try {
  21. return hljs.highlight(lang, str).value;
  22. } catch (err) { console.error(err.stack); }
  23. }
  24. try {
  25. return hljs.highlightAuto(str).value;
  26. } catch (err) { console.error(err.stack); }
  27. return '';
  28. },
  29. });
  30. const frontmatter = fm(source);
  31. frontmatter.attributes.html = md.render(frontmatter.body);
  32. return `module.exports = ${JSON.stringify(frontmatter.attributes)};`;
  33. };