seo 리팩터링, parser renderer 빈등록 해제

This commit is contained in:
jinia91
2022-03-01 19:25:30 +09:00
parent d506b8c393
commit 9d1778a04a
6 changed files with 46 additions and 98 deletions

View File

@@ -13,8 +13,6 @@ import myblog.blog.tags.service.TagsService;
import myblog.blog.tags.dto.TagsDto;
import myblog.blog.layout.LayoutDtoFactory;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.HtmlRenderer;
import org.jsoup.Jsoup;
import org.modelmapper.ModelMapper;
@@ -34,6 +32,8 @@ import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;
import static myblog.blog.utils.MarkdownUtils.*;
@Controller
@RequiredArgsConstructor
public class ArticleController {
@@ -46,8 +46,6 @@ public class ArticleController {
private final LayoutDtoFactory layoutDtoFactory;
private final ModelMapper modelMapper;
private final Parser parser;
private final HtmlRenderer htmlRenderer;
/*
- 아티클 작성 폼 조회
@@ -94,7 +92,7 @@ public class ArticleController {
//
layoutDtoFactory.AddLayoutTo(model);
model.addAttribute("categoryInput", getCategoryDtosForForm());
model.addAttribute("tagsInput", getTagsDtosForForm());;
model.addAttribute("tagsInput", getTagsDtosForForm());
model.addAttribute("articleDto", articleDto);
return "article/articleEditForm";
}
@@ -139,7 +137,7 @@ public class ArticleController {
//
for(ArticleDtoForCardBox articleDto : articleDtoList){
articleDto.setContent(Jsoup.parse(htmlRenderer.render(parser.parse(articleDto.getContent()))).text());
articleDto.setContent(Jsoup.parse(getHtmlRenderer().render(getParser().parse(articleDto.getContent()))).text());
}
layoutDtoFactory.AddLayoutTo(model);
@@ -164,7 +162,7 @@ public class ArticleController {
modelMapper.map(article, ArticleDtoForCardBox.class));
for(ArticleDtoForCardBox article : articleList){
article.setContent(Jsoup.parse(htmlRenderer.render(parser.parse(article.getContent()))).text());
article.setContent(Jsoup.parse(getHtmlRenderer().render(getParser().parse(article.getContent()))).text());
}
PagingBoxDto pagingBoxDto =
@@ -192,7 +190,7 @@ public class ArticleController {
modelMapper.map(article, ArticleDtoForCardBox.class));
for(ArticleDtoForCardBox article : articleList){
article.setContent(Jsoup.parse(htmlRenderer.render(parser.parse(article.getContent()))).text());
article.setContent(Jsoup.parse(getHtmlRenderer().render(getParser().parse(article.getContent()))).text());
}
PagingBoxDto pagingBoxDto =
@@ -244,7 +242,7 @@ public class ArticleController {
.collect(Collectors.toList());
articleDtoForDetail.setTags(tags);
articleDtoForDetail.setContent(htmlRenderer.render(parser.parse(article.getContent())));
articleDtoForDetail.setContent(getHtmlRenderer().render(getParser().parse(article.getContent())));
List<ArticleDtoByCategory> articleTitlesSortByCategory =
articleService

View File

@@ -25,23 +25,6 @@ public class AppConfig {
return modelMapper;
}
/*
- HTML -> 마크다운 파싱 & 렌더러 빈등록
*/
@Bean
public Parser parser(){
return Parser.builder()
.extensions(List.of(TablesExtension.create()))
.build();
}
@Bean
public HtmlRenderer htmlRenderer(){
return HtmlRenderer.builder()
.extensions(List.of(TablesExtension.create()))
.build();
}
@Bean
public Gson gson(){
return new Gson();

View File

@@ -1,36 +1,27 @@
package myblog.blog.main;
import ch.qos.logback.core.Layout;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import myblog.blog.article.dto.ArticleDtoForCardBox;
import myblog.blog.article.service.ArticleService;
import myblog.blog.category.dto.CategoryForView;
import myblog.blog.category.service.CategoryService;
import myblog.blog.comment.dto.CommentDtoForLayout;
import myblog.blog.comment.service.CommentService;
import myblog.blog.layout.LayoutDtoFactory;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.HtmlRenderer;
import org.jsoup.Jsoup;
import org.modelmapper.ModelMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.stream.Collectors;
import static myblog.blog.utils.MarkdownUtils.*;
@Controller
@RequiredArgsConstructor
@Slf4j
public class MainController {
private final ArticleService articleService;
private final HtmlRenderer htmlRenderer;
private final Parser parser;
private final ModelMapper modelMapper;
private final LayoutDtoFactory layoutDtoFactory;
/*
@@ -61,11 +52,11 @@ public class MainController {
.stream()
.map(article -> modelMapper.map(article, ArticleDtoForCardBox.class))
.collect(Collectors.toList());
;
// 화면렌더링을 위한 파싱
for(ArticleDtoForCardBox article : articles){
String content = Jsoup.parse(htmlRenderer.render(parser.parse(article.getContent()))).text();
String content = Jsoup.parse(getHtmlRenderer().render(getParser().parse(article.getContent()))).text();
if(content.length()>300) {
content = content.substring(0, 300);
}

View File

@@ -1,23 +1,17 @@
package myblog.blog.seo.controller;
import lombok.RequiredArgsConstructor;
import myblog.blog.article.domain.Article;
import myblog.blog.article.service.ArticleService;
import myblog.blog.seo.service.RssService;
import myblog.blog.seo.service.SeoFacadeService;
import myblog.blog.seo.service.SiteMapService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Controller
@RequiredArgsConstructor
public class SEOController {
private final SeoFacadeService seoFacadeService;
private final SiteMapService siteMapService;
/*
- rss 피드 발행 요청
@@ -36,7 +30,6 @@ public class SEOController {
*/
@GetMapping(value = "/sitemap",produces = "application/xml;charset=utf-8")
public @ResponseBody String getSiteMap() {
return siteMapService.makeSiteMap();
return seoFacadeService.getSiteMap();
}
}

View File

@@ -3,6 +3,8 @@ package myblog.blog.seo.service;
import lombok.RequiredArgsConstructor;
import myblog.blog.article.domain.Article;
import myblog.blog.article.service.ArticleService;
import myblog.blog.category.domain.Category;
import myblog.blog.category.service.CategoryService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@@ -15,6 +17,7 @@ public class SeoFacadeService {
private final RssService rssService;
private final SiteMapService siteMapService;
private final ArticleService articleService;
private final CategoryService categoryService;
@Cacheable(value = "seoCaching", key = "0")
public String getRssFeed(){
@@ -22,4 +25,11 @@ public class SeoFacadeService {
return rssService.getRssFeed(articles);
}
@Cacheable(value = "seoCaching", key = "1")
public String getSiteMap(){
List<Category> allCategories = categoryService.getAllCategories();
List<Article> articles = articleService.getTotalArticle();
return siteMapService.getSiteMap(articles,allCategories);
}
}

View File

@@ -2,77 +2,54 @@ package myblog.blog.seo.service;
import lombok.RequiredArgsConstructor;
import myblog.blog.article.domain.Article;
import myblog.blog.article.service.ArticleService;
import myblog.blog.category.domain.Category;
import myblog.blog.category.service.CategoryService;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
import org.springframework.cache.annotation.Cacheable;
import org.jdom2.*;
import org.jdom2.output.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.*;
@Service
@Transactional
@RequiredArgsConstructor
public class SiteMapService {
private final ArticleService articleService;
private final CategoryService categoryService;
static final String NAMESPACE = "http://www.sitemaps.org/schemas/sitemap/0.9";
static final String ROOT = "https://www.jiniaslog.co.kr";
static final String CATEGORYPRE = "/article/list?";
static final String CATEGORYPRO = "&page=1";
static final String ARTICLEPREV = "/article/view?articleId=";
/*
- 사이트맵 작성 로직
*/
@Cacheable(value = "seoCaching", key = "1")
public String makeSiteMap(){
List<Article> articles = articleService.getTotalArticle();
List<Category> allCategories = categoryService.getAllCategories();
public String getSiteMap(List<Article> articles, List<Category> allCategories){
Document doc = makeSiteMapDocument(articles, allCategories);
XMLOutputter xmlOutputter = getXmlOutputter();
return xmlOutputter.outputString(doc);
}
// siteMap 레이아웃 루트 작성
private Document makeSiteMapDocument(List<Article> articles, List<Category> allCategories) {
Document doc = new Document();
Element siteMap = new Element("urlset", NAMESPACE);
doc.setRootElement(siteMap);
Element main = createMainElement();
siteMap.addContent(main);
addCategoryUrlsToSiteMap(allCategories, siteMap);
addArticleUrlToSiteMap(articles, siteMap);
return doc;
}
// 메인화면
private Element createMainElement() {
Element main = new Element("url",NAMESPACE);
main.addContent(new Element("loc",NAMESPACE).setText(ROOT));
main.addContent(new Element("lastmod",NAMESPACE).setText(
LocalDateTime.now()
.format(DateTimeFormatter
.ofPattern("yyyy-MM-dd",Locale.ENGLISH))));
LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd",Locale.ENGLISH))));
main.addContent(new Element("priority",NAMESPACE).setText("1.0"));
siteMap.addContent(main);
// url 삽입
addCategoryUrl(allCategories, siteMap);
addArticleUrl(articles, siteMap);
Document doc = new Document();
doc.setRootElement(siteMap);
XMLOutputter xmlOutputter = getXmlOutputter();
return xmlOutputter.outputString(doc);
return main;
}
/*
- 아티클 url 삽입
*/
private void addArticleUrl(List<Article> articles, Element siteMap) {
private void addArticleUrlToSiteMap(List<Article> articles, Element siteMap) {
for (Article article : articles) {
Element articleUrl = new Element("url",NAMESPACE);
articleUrl.addContent(new Element("loc",NAMESPACE)
@@ -81,10 +58,7 @@ public class SiteMapService {
}
}
/*
- 카테고리별 url 삽입
*/
private void addCategoryUrl(List<Category> allCategories, Element siteMap) {
private void addCategoryUrlsToSiteMap(List<Category> allCategories, Element siteMap) {
for (Category category : allCategories) {
Element categoryUrl = new Element("url",NAMESPACE);
categoryUrl.addContent(new Element("loc",NAMESPACE)
@@ -100,5 +74,4 @@ public class SiteMapService {
format.setLineSeparator("\r\n");
return new XMLOutputter(format);
}
}