Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
79 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters