diff --git a/application/src/main/java/run/halo/app/theme/dialect/DuplicateMetaTagProcessor.java b/application/src/main/java/run/halo/app/theme/dialect/DuplicateMetaTagProcessor.java index 6090413727..b9dac2f4dc 100644 --- a/application/src/main/java/run/halo/app/theme/dialect/DuplicateMetaTagProcessor.java +++ b/application/src/main/java/run/halo/app/theme/dialect/DuplicateMetaTagProcessor.java @@ -8,7 +8,6 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import lombok.AllArgsConstructor; -import org.apache.commons.lang3.StringUtils; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; import org.thymeleaf.context.ITemplateContext; @@ -30,7 +29,7 @@ @Component @AllArgsConstructor public class DuplicateMetaTagProcessor implements TemplateHeadProcessor { - static final Pattern META_PATTERN = Pattern.compile(""); + static final Pattern META_PATTERN = Pattern.compile("]+?name=\"([^\"]+)\"[^>]*>\\n*"); @Override public Mono process(ITemplateContext context, IModel model, @@ -49,15 +48,17 @@ public Mono process(ITemplateContext context, IModel model, while (matcher.find()) { String tagLine = matcher.group(0); String nameAttribute = matcher.group(1); - IText metaTagNode = context.getModelFactory().createText(tagLine); + // create a new text node to replace the original text node + // replace multiple line breaks with one line break + IText metaTagNode = context.getModelFactory() + .createText(tagLine.replaceAll("\\n+", "\n")); uniqueMetaTags.put(nameAttribute, new IndexedModel(i, metaTagNode)); text = text.replace(tagLine, ""); } - if (StringUtils.isNotBlank(text)) { - IText otherText = context.getModelFactory() - .createText(text); - otherModel.add(new IndexedModel(i, otherText)); - } + // put the rest of the text into the other model + IText otherText = context.getModelFactory() + .createText(text); + otherModel.add(new IndexedModel(i, otherText)); } else { otherModel.add(new IndexedModel(i, templateEvent)); } diff --git a/application/src/test/java/run/halo/app/theme/dialect/DuplicateMetaTagProcessorTest.java b/application/src/test/java/run/halo/app/theme/dialect/DuplicateMetaTagProcessorTest.java new file mode 100644 index 0000000000..d48e65861e --- /dev/null +++ b/application/src/test/java/run/halo/app/theme/dialect/DuplicateMetaTagProcessorTest.java @@ -0,0 +1,51 @@ +package run.halo.app.theme.dialect; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.regex.Matcher; +import org.junit.jupiter.api.Test; + +/** + * Tests for {@link DuplicateMetaTagProcessor}. + * + * @author guqing + * @since 2.8.0 + */ +class DuplicateMetaTagProcessorTest { + + @Test + void extractMetaTag() { + // normal + String text = ""; + Matcher matcher = DuplicateMetaTagProcessor.META_PATTERN.matcher(text); + assertThat(matcher.find()).isTrue(); + assertThat(matcher.group(1)).isEqualTo("description"); + + // name and content are not in the general order + text = ""; + matcher = DuplicateMetaTagProcessor.META_PATTERN.matcher(text); + assertThat(matcher.find()).isTrue(); + assertThat(matcher.group(1)).isEqualTo("keywords"); + + // no closing slash + text = ""; + matcher = DuplicateMetaTagProcessor.META_PATTERN.matcher(text); + assertThat(matcher.find()).isTrue(); + assertThat(matcher.group(1)).isEqualTo("keywords"); + + // multiple line breaks and other stuff + text = """ + + + + """; + matcher = DuplicateMetaTagProcessor.META_PATTERN.matcher(text); + assertThat(matcher.find()).isTrue(); + assertThat(matcher.group(1)).isEqualTo("description"); + } +} diff --git a/application/src/test/java/run/halo/app/theme/message/ThemeMessageResolverIntegrationTest.java b/application/src/test/java/run/halo/app/theme/message/ThemeMessageResolverIntegrationTest.java index aed217a102..2ce586c917 100644 --- a/application/src/test/java/run/halo/app/theme/message/ThemeMessageResolverIntegrationTest.java +++ b/application/src/test/java/run/halo/app/theme/message/ThemeMessageResolverIntegrationTest.java @@ -72,7 +72,10 @@ void messageResolverWhenDefaultTheme() { .isEqualTo(""" - Title + + + Title + index
zh
@@ -93,7 +96,10 @@ void messageResolverForEnLanguageWhenDefaultTheme() { .isEqualTo(""" - Title + + + Title + index
en
@@ -114,7 +120,10 @@ void shouldUseDefaultWhenLanguageNotSupport() { .isEqualTo(""" - Title + + + Title + index
foo
@@ -135,7 +144,10 @@ void switchTheme() throws URISyntaxException { .isEqualTo(""" - Title + + + Title + index
zh
@@ -154,7 +166,10 @@ void switchTheme() throws URISyntaxException { .isEqualTo(""" - Other theme title + + + Other theme title +

Other 首页

@@ -167,7 +182,10 @@ void switchTheme() throws URISyntaxException { .isEqualTo(""" - Other theme title + + + Other theme title +

other index