Skip to content

Commit

Permalink
fix: ensure href values in not-plaintext tags are removed
Browse files Browse the repository at this point in the history
  • Loading branch information
cossssmin committed Oct 14, 2022
1 parent 24f6419 commit 1ef8e8e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
41 changes: 37 additions & 4 deletions src/generators/plaintext.js
Expand Up @@ -5,6 +5,37 @@ const {stripHtml} = require('string-strip-html')
const defaultConfig = require('./posthtml/defaultConfig')

const self = {
removeCustomTags: (tag, html, config = {}) => {
const posthtmlOptions = get(config, 'build.posthtml.options', {})

const posthtmlPlugin = () => tree => {
const process = node => {
if (!node.tag) {
return node
}

if (node.tag === tag) {
return {
tag: false,
content: ['']
}
}

if (Array.isArray(tag) && tag.includes(node.tag)) {
return {
tag: false,
content: ['']
}
}

return node
}

return tree.walk(process)
}

return posthtml([posthtmlPlugin()]).process(html, {...posthtmlOptions, sync: true}).html
},
handleCustomTags: (html, config = {}) => {
const posthtmlOptions = merge(defaultConfig, get(config, 'build.posthtml.options', {}))

Expand Down Expand Up @@ -32,16 +63,18 @@ const self = {

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

const plaintext = stripHtml(html, {
const strippedHTML = self.removeCustomTags('not-plaintext', html, config)

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

html = self.handleCustomTags(html, config)
Expand Down
2 changes: 1 addition & 1 deletion test/stubs/plaintext/plaintext.html
@@ -1,5 +1,5 @@
<div>Show in HTML</div>
<plaintext>Show in plaintext</plaintext>
<not-plaintext>
<table><tr><td>Remove from plaintext</td></tr></table>
<p>Do not show <a href="url">this</a> in plaintext.</p>
</not-plaintext>
2 changes: 1 addition & 1 deletion test/test-todisk.js
Expand Up @@ -153,7 +153,7 @@ test('outputs plaintext files', async t => {

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'
'<div>Show in HTML</div>\n\n\n <p>Do not show <a href="url">this</a> in plaintext.</p>\n\n'
)
})

Expand Down

0 comments on commit 1ef8e8e

Please sign in to comment.