Skip to content

Commit

Permalink
reader: Clean article wrappers before rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
RangerMauve committed Mar 13, 2024
1 parent eb2aee7 commit 7895274
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions reader.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
/* global DOMParser, document */
const renderPage = require('./template.js')
const {Readability} = require('@mozilla/readability')
const { Readability } = require('@mozilla/readability')

const article = new Readability(document).parse()

const { title, content, byline } = article

let cleanedContent = content

try {
const parsed = (new DOMParser()).parseFromString(content, 'text/html')

// Find the first paragraph
const firstP = parsed.querySelector('p')
const parent = firstP.parentElement
cleanedContent = parent.innerHTML
} catch (e) {
console.error('Unable to clean article', e)
}

const finalContent = `
<h1>${title}</h1>
<h1>${title || document.title}</h1>
${byline ? `<p>${byline}</p>` : ''}
<hr>
${content}
${cleanedContent}
`

renderPage(finalContent, title)

0 comments on commit 7895274

Please sign in to comment.