Merge remote-tracking branch 'origin/feature/post-decoration' into feature/post-decoration

This commit is contained in:
usbharu 2024-01-25 12:22:02 +09:00
commit 82f2742c0c
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))