markdown-loader.ts 506 B

123456789101112131415161718192021
  1. import { loader } from 'webpack'
  2. import LRU from 'lru-cache'
  3. import hash from 'hash-sum'
  4. import markdownIt from './markdown'
  5. const cache = new LRU({ max: 1000 })
  6. export default function (this: loader.LoaderContext, markdown: string) {
  7. // Check cache
  8. const key = hash(this.resourcePath + markdown)
  9. if (cache.has(key)) {
  10. return cache.get(key)
  11. }
  12. // Parse markdown
  13. const md = markdownIt()
  14. const parsedContent = md.render(markdown)
  15. cache.set(key, parsedContent)
  16. return parsedContent
  17. }