47 lines
1.5 KiB
Plaintext
47 lines
1.5 KiB
Plaintext
---
|
|
import { loadRenderers } from "astro:container";
|
|
|
|
// get rendered ArticleBodyContent HTML
|
|
const html = await Astro.slots.render("default");
|
|
|
|
// rule:
|
|
// 1. If a line belongs to the area where the line break should be kept.
|
|
// 2. If a line ends with [a-zA-Z] add a space to the end of the line.
|
|
// 3. If a line does not start in ASCII chars, join it to the previous line.
|
|
// This is important in using one newline character to wrap Chinese.
|
|
const arr = html.split("\n");
|
|
let articleHTMLFinal = arr[0];
|
|
let remainIntactArea = false;
|
|
for (let i = 1; i < arr.length; i++) {
|
|
// The first line of the HTML string is a table-of-content head recognized by
|
|
// remark-toc so it can be ignored.
|
|
if (arr[i].charAt(arr[i].length - 1).match(/[a-zA-Z]/) !== null) {
|
|
arr[i] += " ";
|
|
}
|
|
// Check if the current line belongs to some block area.
|
|
if (arr[i].match(/^(<pre|<code|<blockquote|<table)/) !== null) {
|
|
remainIntactArea = true;
|
|
} else {
|
|
remainIntactArea = false;
|
|
}
|
|
if (
|
|
// (
|
|
// If the first character is not ascii character,
|
|
// OR
|
|
// the final character of previous line is not ascii character.
|
|
// ) AND
|
|
// Current line should not belong to area that remains intact.
|
|
(arr[i].charAt(0).match(/[ -~]/) === null ||
|
|
arr[i - 1].charAt(arr[i - 1].length - 1).match(/[ -~]/) === null) &&
|
|
!remainIntactArea
|
|
) {
|
|
articleHTMLFinal += arr[i];
|
|
} else {
|
|
articleHTMLFinal += "\n";
|
|
articleHTMLFinal += arr[i];
|
|
}
|
|
}
|
|
---
|
|
|
|
<Fragment set:html={articleHTMLFinal} />
|