Skip to content

Commit

Permalink
fix: generate plaintext based on front matter variable
Browse files Browse the repository at this point in the history
  • Loading branch information
cossssmin committed Sep 28, 2022
1 parent ccc2e90 commit 254490e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 21 deletions.
3 changes: 2 additions & 1 deletion src/generators/output/to-disk.js
Expand Up @@ -122,7 +122,8 @@ module.exports = async (env, spinner, config) => {
* tags from the markup before outputting the file.
*/

const plaintextConfig = get(templateConfig, 'plaintext')
// Check if plaintext: true globally, fallback to template's front matter
const plaintextConfig = get(templateConfig, 'plaintext', get(compiled.config, 'plaintext', false))
const plaintextPath = get(plaintextConfig, 'destination.path', config.permalink || file)

if (Boolean(plaintextConfig) || !isEmpty(plaintextConfig)) {
Expand Down
9 changes: 9 additions & 0 deletions test/stubs/plaintext/front-matter.html
@@ -0,0 +1,9 @@
---
plaintext: true
---

<div>Show in HTML</div>
<plaintext>Show in plaintext</plaintext>
<not-plaintext>
<table><tr><td>Remove from plaintext</td></tr></table>
</not-plaintext>
58 changes: 38 additions & 20 deletions test/test-todisk.js
Expand Up @@ -94,7 +94,7 @@ test('outputs files at the correct location if multiple template sources are use
})

t.true(await fs.pathExists(t.context.folder))
t.is(files.length, 3)
t.is(files.length, 5)
})

test('copies all files in the `filetypes` option to destination', async t => {
Expand Down Expand Up @@ -154,24 +154,49 @@ test('outputs plaintext files', async t => {
path: t.context.folder
},
plaintext: true
},
tailwind: {
config: {
purge: false
}
},
extraAttributes: false
})

t.true(files.includes(`${t.context.folder}/plaintext.txt`))

t.is(
await fs.readFile(`${t.context.folder}/plaintext.txt`, 'utf8'),
'Show in HTML\nShow in plaintext'
)

t.is(
await fs.readFile(`${t.context.folder}/plaintext.html`, 'utf8'),
'<div>Show in HTML</div>\n\n\n <table><tr><td>Remove from plaintext</td></tr></table>\n\n'
)
})

test('outputs plaintext files (front matter)', async t => {
const {files} = await Maizzle.build('maizzle-ci', {
fail: 'silent',
build: {
templates: {
source: 'test/stubs/plaintext',
destination: {
path: t.context.folder
}
}
},
extraAttributes: false
})

const plaintext = files.filter(file => file.includes('.txt'))
const html = files.filter(file => file.includes('.html'))
const plaintextContent = await fs.readFile(plaintext[0], 'utf8')
const htmlContent = await fs.readFile(html[0], 'utf8')
t.true(files.includes(`${t.context.folder}/front-matter.txt`))

t.is(plaintext[0], `${t.context.folder}/plaintext.txt`)
t.is(plaintextContent, 'Show in HTML\nShow in plaintext')
t.is(htmlContent, '<div>Show in HTML</div>\n\n\n <table><tr><td>Remove from plaintext</td></tr></table>\n\n')
t.is(
await fs.readFile(`${t.context.folder}/front-matter.txt`, 'utf8'),
'Show in HTML\nShow in plaintext'
)

t.is(
await fs.readFile(`${t.context.folder}/front-matter.html`, 'utf8'),
'<div>Show in HTML</div>\n\n\n <table><tr><td>Remove from plaintext</td></tr></table>\n\n'
)
})

test('outputs plaintext files (custom path)', async t => {
Expand All @@ -188,18 +213,11 @@ test('outputs plaintext files (custom path)', async t => {
path: `${t.context.folder}/nested/plain.text`
}
}
},
tailwind: {
config: {
purge: false
}
}
}
})

const plaintext = files.filter(file => file.includes('.text'))

t.is(plaintext[0], `${t.context.folder}/nested/plain.text`)
t.true(files.includes(`${t.context.folder}/nested/plain.text`))
})

test('renders plaintext string', async t => {
Expand Down

0 comments on commit 254490e

Please sign in to comment.