|
1 | 1 | const { EventEmitter } = require('events')
|
2 | 2 | const { getOptions } = require('loader-utils')
|
3 |
| -const frontmatter = require('yaml-front-matter') |
| 3 | +const yaml = require('yaml-front-matter') |
| 4 | +const { inferTitle } = require('../util') |
4 | 5 |
|
5 |
| -const frontmatterCache = new Map() |
| 6 | +const cache = new Map() |
6 | 7 |
|
7 | 8 | module.exports = function (src) {
|
8 | 9 | const { markdown } = getOptions(this)
|
9 |
| - const parsed = frontmatter.loadFront(src) |
10 |
| - const content = parsed.__content |
11 |
| - delete parsed.__content |
12 | 10 |
|
13 |
| - // diff frontmatter, since it's not going to be part of the returned |
14 |
| - // component, changes in frontmatter do not trigger hot updates |
15 |
| - const serializedData = JSON.stringify(parsed) |
16 |
| - const cachedData = frontmatterCache.get(this.resourcePath) |
17 |
| - if (cachedData != null && cachedData !== serializedData) { |
| 11 | + const frontmatter = yaml.loadFront(src) |
| 12 | + const inferredTitle = inferTitle(frontmatter) |
| 13 | + const content = frontmatter.__content |
| 14 | + delete frontmatter.__content |
| 15 | + |
| 16 | + // diff frontmatter and title, since they are not going to be part of the |
| 17 | + // returned component, changes in frontmatter do not trigger proper updates |
| 18 | + const cachedData = cache.get(this.resourcePath) |
| 19 | + if (cachedData && ( |
| 20 | + cachedData.inferredTitle !== inferredTitle || |
| 21 | + JSON.stringify(cachedData.frontmatter) !== JSON.stringify(frontmatter) |
| 22 | + )) { |
18 | 23 | // frontmatter changed... need to do a full reload
|
19 | 24 | module.exports.frontmatterEmitter.emit('update')
|
20 | 25 | }
|
21 |
| - frontmatterCache.set(this.resourcePath, serializedData) |
| 26 | + |
| 27 | + cache.set(this.resourcePath, { |
| 28 | + frontmatter, |
| 29 | + inferredTitle |
| 30 | + }) |
22 | 31 |
|
23 | 32 | const { html, hoistedTags } = markdown.renderWithHoisting(content)
|
24 | 33 | return (
|
|
0 commit comments