Skip to content

Commit

Permalink
feat: not-plaintext tag
Browse files Browse the repository at this point in the history
  • Loading branch information
cossssmin committed Sep 28, 2022
1 parent 3b78de9 commit ccc2e90
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 71 deletions.
8 changes: 4 additions & 4 deletions src/generators/output/to-disk.js
Expand Up @@ -3,7 +3,6 @@ const fs = require('fs-extra')
const glob = require('glob-promise')
const {get, isEmpty, merge} = require('lodash')
const {asyncForEach} = require('../../utils/helpers')
const removePlaintextTags = require('../../transformers/plaintext')

const Config = require('../config')
const Tailwind = require('../tailwindcss')
Expand Down Expand Up @@ -133,11 +132,12 @@ module.exports = async (env, spinner, config) => {
plaintextPath,
merge(plaintextConfig, {filepath: file})
)
.then(({plaintext, destination}) => fs.outputFile(destination, plaintext))
.then(async ({html, plaintext, destination}) => {
compiled.html = html
await fs.outputFile(destination, plaintext)
})
}

compiled.html = removePlaintextTags(compiled.html, config)

/**
* Output file
*/
Expand Down
103 changes: 69 additions & 34 deletions src/generators/plaintext.js
@@ -1,49 +1,84 @@
const path = require('path')
const {get} = require('lodash')
const posthtml = require('posthtml')
const {stripHtml} = require('string-strip-html')

module.exports.generate = async (html, destination, config) => {
const configDestinationPath = get(config, 'destination.path')
const extension = get(config, 'destination.extension', 'txt')
const self = {
handleCustomTags: (html, config = {}) => {
const posthtmlOptions = get(config, 'build.posthtml.options', {})

const plaintext = stripHtml(html, {
dumpLinkHrefsNearby: {
enabled: true
},
...get(config, 'options', {})
}).result
const posthtmlPlugin = () => tree => {
const process = node => {
if (node.tag === 'plaintext') {
return {
tag: false,
content: ['']
}
}

// If we set plaintext.destination.path in config/fm
if (configDestinationPath) {
/**
* Using a file path will generate a single plaintext file,
* no matter how many templates there are.
*
* It will be based on the last-processed template.
*/
if (path.extname(configDestinationPath)) {
destination = configDestinationPath
if (node.tag === 'not-plaintext') {
return {
tag: false,
content: tree.render(node.content)
}
}

return node
}

return tree.walk(process)
}

return posthtml([posthtmlPlugin()]).process(html, {...posthtmlOptions, sync: true}).html
},
generate: async (html, destination, config) => {
const configDestinationPath = get(config, 'destination.path')
const extension = get(config, 'destination.extension', 'txt')

const plaintext = stripHtml(html, {
dumpLinkHrefsNearby: {
enabled: true
},
stripTogetherWithTheirContents: ['script', 'style', 'xml', 'not-plaintext'],
...get(config, 'options', {})
}).result

html = self.handleCustomTags(html, config)

return {plaintext, destination}
// If we set plaintext.destination.path in config/fm
if (configDestinationPath) {
/**
* Using a file path will generate a single plaintext file,
* no matter how many templates there are.
*
* It will be based on the last-processed template.
*/
if (path.extname(configDestinationPath)) {
destination = configDestinationPath

return {html, plaintext, destination}
}

/**
* Using a directory-like path for plaintext.destination.path
*/
destination = path.join(configDestinationPath, path.basename(config.filepath, path.extname(config.filepath)) + '.' + extension)

return {html, plaintext, destination}
}

/**
* Using a directory-like path for plaintext.destination.path
* Use template's `permalink` Front Matter key,
* fall back to the original `destination`.
*/
destination = path.join(configDestinationPath, path.basename(config.filepath, path.extname(config.filepath)) + '.' + extension)

return {plaintext, destination}
}
destination = get(config, 'permalink', destination)

/**
* Use template's `permalink` Front Matter key,
* fall back to the original `destination`.
*/
destination = get(config, 'permalink', destination)
if (typeof destination === 'string') {
destination = path.join(path.dirname(destination), path.basename(destination, path.extname(destination)) + '.' + extension)
}

if (typeof destination === 'string') {
destination = path.join(path.dirname(destination), path.basename(destination, path.extname(destination)) + '.' + extension)
return {html, plaintext, destination}
}

return {plaintext, destination}
}

module.exports = self
23 changes: 0 additions & 23 deletions src/transformers/plaintext.js

This file was deleted.

3 changes: 3 additions & 0 deletions test/stubs/plaintext/plaintext.html
@@ -1,2 +1,5 @@
<div>Show in HTML</div>
<plaintext>Show in plaintext</plaintext>
<not-plaintext>
<table><tr><td>Remove from plaintext</td></tr></table>
</not-plaintext>
5 changes: 3 additions & 2 deletions test/test-todisk.js
Expand Up @@ -160,7 +160,8 @@ test('outputs plaintext files', async t => {
purge: false
}
}
}
},
extraAttributes: false
})

const plaintext = files.filter(file => file.includes('.txt'))
Expand All @@ -170,7 +171,7 @@ test('outputs plaintext files', async t => {

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')
t.is(htmlContent, '<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 Down
8 changes: 0 additions & 8 deletions test/test-transformers.js
@@ -1,6 +1,5 @@
const test = require('ava')
const Maizzle = require('../src')
const removePlaintextTags = require('../src/transformers/plaintext')

const path = require('path')
const fs = require('fs')
Expand Down Expand Up @@ -232,13 +231,6 @@ test('minify (disabled)', async t => {
t.is(html, '<div>\n\n<p>\n\ntest</p></div>')
})

test('removes plaintext tag', t => {
let html = removePlaintextTags('<plaintext>Removed</plaintext><div>Preserved</div>')
html = html.replace(/[^\S\r\n]+$/gm, '').trim()

t.is(html, '<div>Preserved</div>')
})

test('replace strings', async t => {
const html = await Maizzle.replaceStrings('initial text', {initial: 'updated'})

Expand Down

0 comments on commit ccc2e90

Please sign in to comment.