Skip to content

Commit

Permalink
fix(resolver): cleanup unused class when saving revised content
Browse files Browse the repository at this point in the history
  • Loading branch information
devformatters authored and Zeck Li committed Nov 9, 2020
1 parent 71818c4 commit 86b19d3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
11 changes: 11 additions & 0 deletions src/common/utils/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,14 @@ export const outputCleanHTML = (html: string) => {

return $.html()
}

/**
* Strip specific class from html string
*/
export const stripClass = (html: string, name: string) => {
const $ = cheerio.load(html, { decodeEntities: false, xmlMode: true })

$(`.${name}`).removeClass(name)

return $.html()
}
11 changes: 7 additions & 4 deletions src/mutations/article/editArticle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
makeSummary,
measureDiffs,
sanitize,
stripClass,
stripHtml,
} from 'common/utils'
import { revisionQueue } from 'connectors/queue'
Expand Down Expand Up @@ -222,10 +223,12 @@ const resolver: MutationToEditArticleResolver = async (
)
}

const cleanedContent = stripClass(content, 'u-area-disable')

// check diff distances reaches limit or not
const diffs = measureDiffs(
stripHtml(currDraft.content, ''),
stripHtml(content, '')
stripHtml(cleanedContent, '')
)
if (diffs > 50) {
throw new ArticleRevisionContentInvalidError('revised content invalid')
Expand All @@ -248,12 +251,12 @@ const resolver: MutationToEditArticleResolver = async (
authorId: currDraft.authorId,
articleId: currArticle.id,
title: currDraft.title,
summary: makeSummary(content),
content: sanitize(content),
summary: makeSummary(cleanedContent),
content: sanitize(cleanedContent),
tags: currTagContents,
cover: currArticle.cover,
collection: currCollectionIds,
wordCount: countWords(content),
wordCount: countWords(cleanedContent),
archived: false,
publishState: PUBLISH_STATE.pending,
createdAt: new Date(),
Expand Down

0 comments on commit 86b19d3

Please sign in to comment.