Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure href values in not-plaintext tags are removed #803

Merged
merged 1 commit into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 37 additions & 4 deletions src/generators/plaintext.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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