Typora導(dǎo)出html圖片轉(zhuǎn)base64
Typora 導(dǎo)出html圖片轉(zhuǎn)base64
Typora中圖片使用絕對路徑- 圖片路徑不要使用中文,否則可能會不成功
- 打包
jar,jar放在到出 html 同級目錄下- 必須要有
jdk環(huán)境
一、實現(xiàn)代碼
xxxxxxxxxx
import java.io.*;
import java.util.Base64;
public class Main {
/**
* @param src img src 內(nèi)容
* @param end 下次查找字符串起始位置
* @return java.lang.String
* @throws
* @description 遞歸執(zhí)行查找同一行字符串多個 img 標(biāo)簽
* @date 2021/10/1 11:07
* @Author Mr.Fang
*/
public static String execute(String src, int end) {
String result = matchImg(src, end);
System.out.println("文件路徑:-----" + result);
if (result.isEmpty()) {
return src;
} else {
String[] split = result.split(",");
String s1 = fileToBase64(split[0]);
if (s1.isEmpty()) {
return src;
} else {
String replace = src.replace(split[0], s1);
return execute(replace, Integer.valueOf(split[1]) + 20);
}
}
}
/**
* @param str 原始字符串
* @return java.lang.String
* @Description 匹配 img src 內(nèi)容
* @date 2021/9/30 0030 16:32
* @auther Mr.Fang
**/
public static String matchImg(String str, int start) {
int img = str.indexOf("<img", start); // 起始位置
if (img == -1) {
return "";
}
int l = str.indexOf("\"", img) + 1; // src 左側(cè) 雙引號
int r = str.indexOf("\"", l); // src 右側(cè) 雙引號
String substring = str.substring(l, r);
if (substring.startsWith("data")) { // 跳過已經(jīng) base64 編碼的文件 和 http 地址
return matchImg(str, r);
}
return substring + "," + r; // src 地址 返回 src 內(nèi)容以及最后的位置 使用逗號拼接
}
/**
* @param path 文件路徑
* @return java.lang.String
* @Description 文件轉(zhuǎn) base64
* @date 2021/9/30 0030 16:37
* @auther Mr.Fang
**/
public static String fileToBase64(String path) {
File file = new File(path);
if (!file.exists()) {
System.err.printf("文件不存在");
return "";
}
byte bytes[] = null;
try (FileInputStream fileInputStream = new FileInputStream(path);) {
bytes = new byte[fileInputStream.available()];
fileInputStream.read(bytes);
} catch (Exception e) {
e.printStackTrace();
System.err.println("圖片轉(zhuǎn) base64 失敗");
}
// 文件后綴處理
String suffix = getSuffix(path);
return "data:image/" + suffix + ";base64," + Base64.getEncoder().encodeToString(bytes);
}
/**
* @param str
* @return java.lang.String
* @throws
* @description 獲取文件后綴
* @date 2021/10/1 16:43
* @Author Mr.Fang
*/
public static String getSuffix(String str) {
return str.substring(str.lastIndexOf(".") + 1);
}
// 主方法
public static void main(String[] args) {
// 獲取文件路徑
if (args.length == 0) {
System.out.println("No parameters passed");
return;
}
String arg = args[0];
// 獲取文件后綴
String suffix = getSuffix(arg);
try (BufferedReader bfr = new BufferedReader(new FileReader(arg));
BufferedWriter bfw = new BufferedWriter(new FileWriter(arg.concat("-bast64.").concat(suffix)))
) {
String len = "";
while ((len = bfr.readLine()) != null) {
String result = "";
if (len.indexOf("<img") != -1) {
result = execute(len, 0);
}
if (result.equals("")) {
bfw.write(len);
} else {
bfw.write(result);
}
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("error");
}
System.out.println("success");
}
}
二、打包 jar











三、cmd 執(zhí)行 jar
- 到出需要本地圖片轉(zhuǎn)
base64的html文件- typora-img.jar 放到 文件同級目錄下
- 執(zhí)行命令
java -jar typora-img.jar加文件絕對路徑E:\makerdown\測試圖片轉(zhuǎn)換PDF.html
xxxxxxxxxx
java -jar typora-img.jar E:\makerdown\測試圖片轉(zhuǎn)換PDF.html

四、轉(zhuǎn)換后結(jié)果

五、typora 配置
typora提供導(dǎo)出后執(zhí)行自定義命令- 左上角 文件->偏好設(shè)置->導(dǎo)出->html->導(dǎo)出后運行自定義命令
x
java -jar typora-img.jar "${outputPath}"

六、其他參數(shù)
更多參數(shù)信息查看官網(wǎng) https://support.typora.io/Export/
您可以在自定義頁眉/頁腳文本和自定義導(dǎo)出命令中使用 ${variables},它們的值是:
| Key | Value |
|---|---|
${outputPath} |
Output file path after export. For example, if you export to location /User/aaa/Documents/test.pdf, then ${outputPath} will be replaced to that path. |
${outputFileName} |
File name (without extension) of the saved exported file. It will be test in above case. |
${outputFileFullName} |
File name (with extension) of the saved exported file. It will be test.md in above case. |
${currentPath} |
Path of currently edited file. For example, if you are editing /User/aaa/Document/readme.md, then the value will be /User/aaa/Document/readme.md. |
${currentFileName} |
Filename without extension of currently edited file. It will be readme in above case. |
${currentFileFullName} |
Filename with extension of currently edited file. It will be readme.md in above case. |
${today} |
Current date, for example: 2020-01-19 |
${pageNo} |
Current page number. Only available for PDF format. |
${pageCount} / ${totalPages} |
Total page counts. Only available for PDF format. |
${title} |
Article title, should be defined in YAML Front Matter. |
${author} |
Article author, defined in export options for PDF format, can be overwritten in YAML Front Matter. |
${a.b} |
If a is an object defined in YAML Front Matter which contains b, then you can use a.b to access value for b. |
| Other variables | You can use keyword: value in YAML Front Matter, then uses ${keyword} variables in export configs. |
其他
typora 官網(wǎng) https://www.typora.io/
jar下載地址 https://864000.lanzoui.com/iRX5suqya6b
哇!又賺了一天人民幣

浙公網(wǎng)安備 33010602011771號