|
| 1 | +import { compressTree } from '@nuxt/content/runtime' |
1 | 2 | import { type DatabasePageItem, ContentFileExtension } from '../types' |
2 | 3 | import { stringify } from 'minimark/stringify' |
| 4 | +import { MDCRoot } from '@nuxtjs/mdc' |
| 5 | +import { MarkdownRoot } from '@nuxt/content' |
3 | 6 |
|
4 | 7 | export function isEqual(document1: DatabasePageItem, document2: DatabasePageItem) { |
5 | | - function removeLastStyle(document: DatabasePageItem) { |
6 | | - if (document.body?.value[document.body?.value.length - 1]?.[0] === 'style') { |
7 | | - return { ...document, body: { ...document.body, value: document.body?.value.slice(0, -1) } } |
| 8 | + function withoutLastStyles(body: MarkdownRoot) { |
| 9 | + if (body.value[body.value.length - 1]?.[0] === 'style') { |
| 10 | + return { ...body, value: body.value.slice(0, -1) } |
8 | 11 | } |
9 | | - return document |
| 12 | + return body |
10 | 13 | } |
11 | 14 |
|
12 | 15 | const { body: body1, meta: meta1, ...documentData1 } = document1 |
13 | 16 | const { body: body2, meta: meta2, ...documentData2 } = document2 |
14 | 17 |
|
15 | 18 | // Compare body first |
16 | 19 | if (document1.extension === ContentFileExtension.Markdown) { |
17 | | - if (document1.body?.type === 'minimark') { |
18 | | - document1 = removeLastStyle(document1) |
19 | | - } |
20 | | - |
21 | | - if (document2.body?.type === 'minimark') { |
22 | | - document2 = removeLastStyle(document2) |
23 | | - } |
24 | | - |
25 | | - if (stringify(body1) !== stringify(body2)) { |
| 20 | + const minifiedBody1 = withoutLastStyles( |
| 21 | + document1.body.type === 'minimark' ? document1.body : compressTree(document1.body as unknown as MDCRoot) |
| 22 | + ) |
| 23 | + const minifiedBody2 = withoutLastStyles( |
| 24 | + document2.body.type === 'minimark' ? document2.body : compressTree(document2.body as unknown as MDCRoot) |
| 25 | + ) |
| 26 | + |
| 27 | + if (stringify(minifiedBody1) !== stringify(minifiedBody2)) { |
26 | 28 | return false |
27 | 29 | } |
28 | 30 | } |
|
0 commit comments