之前使用python库实现了一版拉取cf内容转换为markdown
现在使用java实现一版并封装成接口
0、安装相关库
主要使用到了两个库
jsoup:对html进行处理,类似python的BeautifulSoup
flexmark-all:将html转换为markdown
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.15.4</version>
</dependency>
<dependency>
<groupId>com.vladsch.flexmark</groupId>
<artifactId>flexmark-all</artifactId>
<version>0.62.2</version>
</dependency>
1、拿到页面内容
调用rest接口拿到数据
String host = "https://cf.qunhequnhe.com";
String pageInfoUrl = host + "/rest/api/content/" + pageId + "?expand=body.storage";
例如:
https://cf.qunhequnhe.com/rest/api/content/80978298926?expand=body.storage
其中
标题是:title
内容为:body.storage.value
2、拿到附件地址(主要是图片)
需要将拿到的图片进行上传,上传后的图片可以使用外网cdn地址访问
String pageAttachmentUrl = host + "/rest/api/content/" + pageId + "/child/attachment?start=0&limit=500";
例如:
https://cf.qunhequnhe.com/rest/api/content/80978298926/child/attachment?start=0\&limit=500
其中
标题是:results[x].title
图片链接是:results[x]._links.download
将结果存到一个字典中,等待后续使用
3、处理HTML
直接拿到的html中有很多cf专有的内容需要进行转换
先使用jsoup处理拿到的html
Document doc = Jsoup.parse(pageContentHtml);
3.1、图片处理
<ac:image ac:thumbnail="true" ac:height="86"><ri:attachment ri:filename="image2024-6-27_18-14-6.png" /></ac:image>
找到特殊的图片然后进行for循环逐个处理
拿到图片名称
根据图片名称从附件字典中进行下载,再上传到存储服务中,拿到外网可访问的图片链接地址
替换图片链接
替换标签
Elements images = doc.select("ac\\:image");
for (Element image : images) {
String fileName = null;
String key = null;
for (Element child : image.children()) {
fileName = child.attr("ri:filename");
if ("null".equals(fileName) || fileName.isEmpty()) {
fileName = child.attr("ri:value");
}
break;
}
if (fileName == null) {
continue;
}
String imgUrl = imgHashMap.get(fileName);
if (fileName.startsWith("http")) {
httpGet = new HttpGet(fileName);
} else {
httpGet = new HttpGet(host + imgUrl);
}
httpGet.addHeader("Authorization", "Bearer " + token);
httpGet.addHeader("Cookie", cookie);
httpClient = HttpClientBuilder.create().build();
try {
CloseableHttpResponse imgResponse = httpClient.execute(httpGet);
HttpEntity imageEntity = imgResponse.getEntity();
byte[] imageData = EntityUtils.toByteArray(imageEntity);
// 上传图片并拿到新的图片url(省略这部分...)
String srcUrl = info.getDomainWithProtocol() + "/" + key;
Element imgTag = doc.createElement("img");
imgTag.attr("src", srcUrl);
imgTag.attr("alt", fileName);
image.after(doc.createElement("br"));
image.replaceWith(imgTag);
} catch (IOException e) {
log.error("处理图片异常", e);
}
}
3.2、代码处理
Elements codes = doc.select("ac\\:structured-macro[ac:name=code]");
for (Element code : codes) {
code.replaceWith(new Element("pre").text(code.text()));
}
3.3、Kaptain链接处理
Elements kaptains = doc.select("ac\\:structured-macro[ac:name=kaptain-issue]");
for (Element kaptain : kaptains) {
kaptain.replaceWith(new Element("p").text("Kaptain事项:" + kaptain.text()));
}
3.4、待办事项处理
没找到好的html转换先转成列表
Elements taskLists = doc.select("ac\\:task-list");
for (Element taskList : taskLists) {
taskList.tagName("ul");
Elements tasks = doc.select("ac\\:task");
for (Element task : tasks) {
task.replaceWith(new Element("li").text(task.select("ac\\:task-body").text()));
}
}
3.5、CF链接处理
这里先简单处理了,没有去cf搜索具体的链接,先将链接变成了一个没有路径的a标签
Elements links = doc.select("ac\\:link");
for (Element link : links) {
String linkName = link.select("ri\\:page").attr("ri:content-title").trim();
if (!linkName.isEmpty()) {
link.replaceWith(new Element("a").text(linkName).attr("href", ""));
}
}
3.6、展开处理
Elements expands = doc.select("ac\\:structured-macro[ac:name=expand]");
for (Element expand : expands) {
expand.tagName("div");
expand.attributes().forEach(item -> expand.removeAttr(item.getKey()));
expand.attributes().forEach(item -> expand.removeAttr(item.getKey()));
expand.select("ac\\:parameter[ac:name=title]").tagName("span").removeAttr("ac:name");
expand.select("ac\\:rich-text-body").tagName("div");
}
4、 转换为Markdown
FlexmarkHtmlConverter converter = FlexmarkHtmlConverter.builder().build();
String convert = converter.convert(doc.toString());