Skip to content

Commit

Permalink
fix: do not flatted data in fm stringify (#70)
Browse files Browse the repository at this point in the history
All fm data should be written in standard YAML format
  • Loading branch information
farnabaz authored Jan 22, 2024
1 parent ed7e91b commit d58f607
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 13 deletions.
10 changes: 2 additions & 8 deletions src/frontmatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,12 @@ import * as flat from 'flat'
const FRONTMATTER_DELIMITER = '---'

export function stringifyFrontMatter (data: any, content = '') {
// flatten frontmatter data
// convert `parent: { child: ... }` into flat keys `parent.child`
data = flat.flatten(data, {
// preserve arrays and their contents as is and do not walk through arrays
// flatten array will be like `parent.0.child` and `parent.1.child` with is not readable
safe: true
})

if (!Object.keys(data).length) {
return ''
}

data = flat.unflatten(data || {}, {})

return [
FRONTMATTER_DELIMITER,
yaml.dump(data, { lineWidth: -1 }).trim(),
Expand Down
2 changes: 1 addition & 1 deletion src/to-markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { containerFlow, containerPhrasing, checkQuote } from './mdast-util-to-ma
import { stringifyFrontMatter } from './frontmatter'
import type { RemarkMDCOptions } from './types'
import { NON_UNWRAPPABLE_TYPES } from './utils'
import { Container } from './micromark-extension/types'
import { type Container } from './micromark-extension/types'

const own = {}.hasOwnProperty

Expand Down
177 changes: 177 additions & 0 deletions test/__snapshots__/basic.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`basic > link 1`] = `
{
"children": [
{
"children": [
{
"attributes": {
"target": "_blank",
},
"children": [
{
"position": {
"end": {
"column": 6,
"line": 1,
"offset": 5,
},
"start": {
"column": 2,
"line": 1,
"offset": 1,
},
},
"type": "text",
"value": "link",
},
],
"position": {
"end": {
"column": 27,
"line": 1,
"offset": 26,
},
"start": {
"column": 1,
"line": 1,
"offset": 0,
},
},
"title": null,
"type": "link",
"url": "https://nuxtjs.org",
},
],
"position": {
"end": {
"column": 44,
"line": 1,
"offset": 43,
},
"start": {
"column": 1,
"line": 1,
"offset": 0,
},
},
"type": "paragraph",
},
],
"position": {
"end": {
"column": 44,
"line": 1,
"offset": 43,
},
"start": {
"column": 1,
"line": 1,
"offset": 0,
},
},
"type": "root",
}
`;

exports[`basic > simple 1`] = `
{
"children": [
{
"children": [
{
"position": {
"end": {
"column": 12,
"line": 1,
"offset": 11,
},
"start": {
"column": 1,
"line": 1,
"offset": 0,
},
},
"type": "text",
"value": "paragraph 1",
},
],
"position": {
"end": {
"column": 12,
"line": 1,
"offset": 11,
},
"start": {
"column": 1,
"line": 1,
"offset": 0,
},
},
"type": "paragraph",
},
{
"position": {
"end": {
"column": 4,
"line": 3,
"offset": 16,
},
"start": {
"column": 1,
"line": 3,
"offset": 13,
},
},
"type": "thematicBreak",
},
{
"children": [
{
"position": {
"end": {
"column": 12,
"line": 5,
"offset": 29,
},
"start": {
"column": 1,
"line": 5,
"offset": 18,
},
},
"type": "text",
"value": "paragraph 2",
},
],
"position": {
"end": {
"column": 12,
"line": 5,
"offset": 29,
},
"start": {
"column": 1,
"line": 5,
"offset": 18,
},
},
"type": "paragraph",
},
],
"position": {
"end": {
"column": 12,
"line": 5,
"offset": 29,
},
"start": {
"column": 1,
"line": 1,
"offset": 0,
},
},
"type": "root",
}
`;
2 changes: 1 addition & 1 deletion test/attributes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('Attributes', () => {
markdown: '[Nuxt](https://nuxtjs.org){#id .class}'
},
emphasis: {
markdown: '*emphasis*{#id .class}'
markdown: '_emphasis_{#id .class}'
}
})
})
31 changes: 31 additions & 0 deletions test/basic.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { describe } from 'vitest'
import { runMarkdownTests } from './utils'

describe('basic', () => {
runMarkdownTests({
simple: {
mdcOptions: {
experimental: {
autoUnwrap: true
}
},
markdown: [
'paragraph 1',
'',
'---',
'',
'paragraph 2'
].join('\n')
},
link: {
mdcOptions: {
experimental: {
autoUnwrap: true
}
},
markdown: [
'[link](https://nuxtjs.org){target="_blank"}'
].join('\n')
}
})
})
5 changes: 3 additions & 2 deletions test/block-component.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ describe('block-component', () => {
'::with-frontmatter',
'---',
'key: value',
'key2.subkey: value',
'key2.subkey2: value',
'key2:',
' subkey: value',
' subkey2: value',
'array:',
' - item',
' - itemKey: value',
Expand Down
7 changes: 6 additions & 1 deletion test/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,12 @@ async function astToMarkdown (ast: any, plugins = [] as any[], mdcOptions = {})
stream.use(plugin)
}
stream.use(stringify, {
bullet: '-'
bullet: '-',
emphasis: '_',
listItemIndent: 'one',
fence: '`',
fences: true,
rule: '-'
})
const result = await stream.process(JSON.stringify(ast))
return result.value as string
Expand Down

0 comments on commit d58f607

Please sign in to comment.