Apply suggestions from code review

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
This commit is contained in:
usbharu 2024-01-25 12:12:02 +09:00 committed by GitHub
parent 06ce40991c
commit 5a4f6c0733
1 changed files with 3 additions and 8 deletions

View File

@ -11,8 +11,7 @@ import org.springframework.stereotype.Service
@Service @Service
class DefaultPostContentFormatter(private val policyFactory: PolicyFactory) : PostContentFormatter { class DefaultPostContentFormatter(private val policyFactory: PolicyFactory) : PostContentFormatter {
override fun format(content: String): FormattedPostContent { override fun format(content: String): FormattedPostContent {
// まず不正なHTMLを整形する
//まず不正なHTMLを整形する
val document = Jsoup.parseBodyFragment(content) val document = Jsoup.parseBodyFragment(content)
val outputSettings = Document.OutputSettings() val outputSettings = Document.OutputSettings()
outputSettings.prettyPrint(false) outputSettings.prettyPrint(false)
@ -24,8 +23,7 @@ class DefaultPostContentFormatter(private val policyFactory: PolicyFactory) : Po
"" ""
) )
// 文字だけのHTMLなどはここでpタグで囲む
//文字だけのHTMLなどはここでpタグで囲む
val flattenHtml = unsafeElement.childNodes().mapNotNull { val flattenHtml = unsafeElement.childNodes().mapNotNull {
if (it is Element) { if (it is Element) {
it it
@ -36,7 +34,6 @@ class DefaultPostContentFormatter(private val policyFactory: PolicyFactory) : Po
} }
}.filter { it.text().isNotBlank() } }.filter { it.text().isNotBlank() }
// HTMLのサニタイズをする // HTMLのサニタイズをする
val unsafeHtml = Elements(flattenHtml).outerHtml() val unsafeHtml = Elements(flattenHtml).outerHtml()
@ -47,8 +44,7 @@ class DefaultPostContentFormatter(private val policyFactory: PolicyFactory) : Po
val formattedHtml = mutableListOf<Element>() val formattedHtml = mutableListOf<Element>()
// 連続するbrタグを段落に変換する
//連続するbrタグを段落に変換する
for (element in safeDocument.children()) { for (element in safeDocument.children()) {
var brCount = 0 var brCount = 0
var prevIndex = 0 var prevIndex = 0
@ -64,7 +60,6 @@ class DefaultPostContentFormatter(private val policyFactory: PolicyFactory) : Po
formattedHtml.add(Element("p").appendChildren(childNodes.subList(prevIndex, childNodes.size))) formattedHtml.add(Element("p").appendChildren(childNodes.subList(prevIndex, childNodes.size)))
} }
val elements = Elements(formattedHtml) val elements = Elements(formattedHtml)
return FormattedPostContent(elements.outerHtml().replace("\n", ""), printHtml(elements)) return FormattedPostContent(elements.outerHtml().replace("\n", ""), printHtml(elements))