forked from halo-dev/halo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add generator meta into head (halo-dev#4821)
#### What type of PR is this? /kind feature /area core /milestone 2.11.x #### What this PR does / why we need it: Please see https://html.spec.whatwg.org/multipage/semantics.html#meta-generator for more. This PR add the generator meta into head, so that we can know what sites are using Halo. ```bash http localhost:8090/ | grep generator <meta name="generator" content="Halo 2.11.0-SNAPSHOT"/> ``` If someone want to disable the generator meta, they can configure the property `halo.theme.generator-meta-disabled` to `true`. #### Does this PR introduce a user-facing change? ```release-note 添加 Generator 元数据标识 ```
- Loading branch information
Showing
7 changed files
with
143 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
application/src/main/java/run/halo/app/theme/dialect/GeneratorMetaProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package run.halo.app.theme.dialect; | ||
|
||
import static org.thymeleaf.model.AttributeValueQuotes.DOUBLE; | ||
|
||
import java.util.Map; | ||
import org.springframework.beans.factory.ObjectProvider; | ||
import org.springframework.boot.info.BuildProperties; | ||
import org.springframework.core.annotation.Order; | ||
import org.thymeleaf.context.ITemplateContext; | ||
import org.thymeleaf.model.IModel; | ||
import org.thymeleaf.processor.element.IElementModelStructureHandler; | ||
import reactor.core.publisher.Mono; | ||
|
||
/** | ||
* Processor for generating generator meta. | ||
* Set the order to 0 for removing the meta in later TemplateHeadProcessor. | ||
* | ||
* @author johnniang | ||
*/ | ||
@Order(0) | ||
public class GeneratorMetaProcessor implements TemplateHeadProcessor { | ||
|
||
private final String generatorValue; | ||
|
||
public GeneratorMetaProcessor(ObjectProvider<BuildProperties> buildProperties) { | ||
this.generatorValue = "Halo " + buildProperties.stream().findFirst() | ||
.map(BuildProperties::getVersion) | ||
.orElse("Unknown"); | ||
} | ||
|
||
@Override | ||
public Mono<Void> process(ITemplateContext context, IModel model, | ||
IElementModelStructureHandler structureHandler) { | ||
return Mono.fromRunnable(() -> { | ||
var modelFactory = context.getModelFactory(); | ||
var generatorMeta = modelFactory.createStandaloneElementTag("meta", | ||
Map.of("name", "generator", "content", generatorValue), | ||
DOUBLE, false, true); | ||
model.add(generatorMeta); | ||
}); | ||
} | ||
|
||
} |
59 changes: 59 additions & 0 deletions
59
application/src/test/java/run/halo/app/theme/dialect/GeneratorMetaProcessorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package run.halo.app.theme.dialect; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.io.FileNotFoundException; | ||
import java.net.URISyntaxException; | ||
import java.nio.file.Path; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.test.web.reactive.server.WebTestClient; | ||
import org.springframework.util.ResourceUtils; | ||
import org.springframework.web.server.ServerWebExchange; | ||
import reactor.core.publisher.Mono; | ||
import run.halo.app.infra.InitializationStateGetter; | ||
import run.halo.app.theme.ThemeContext; | ||
import run.halo.app.theme.ThemeResolver; | ||
|
||
@SpringBootTest | ||
@AutoConfigureWebTestClient | ||
class GeneratorMetaProcessorTest { | ||
|
||
@Autowired | ||
WebTestClient webClient; | ||
|
||
@MockBean | ||
InitializationStateGetter initializationStateGetter; | ||
|
||
@MockBean | ||
ThemeResolver themeResolver; | ||
|
||
@BeforeEach | ||
void setUp() throws FileNotFoundException, URISyntaxException { | ||
when(initializationStateGetter.userInitialized()).thenReturn(Mono.just(true)); | ||
var themeContext = ThemeContext.builder() | ||
.name("default") | ||
.path(Path.of(ResourceUtils.getURL("classpath:themes/default").toURI())) | ||
.active(true) | ||
.build(); | ||
when(themeResolver.getTheme(any(ServerWebExchange.class))) | ||
.thenReturn(Mono.just(themeContext)); | ||
} | ||
|
||
@Test | ||
void requestIndexPage() { | ||
webClient.get().uri("/") | ||
.exchange() | ||
.expectStatus().isOk() | ||
.expectBody() | ||
.consumeWith(System.out::println) | ||
.xpath("/html/head/meta[@name=\"generator\"][starts-with(@content, \"Halo \")]") | ||
.exists(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
application/src/test/resources/themes/default/templates/index.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
application/src/test/resources/themes/other/templates/index.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters