From 9e33f42d34bbf16f1b02199ca96035114ddc9200 Mon Sep 17 00:00:00 2001 From: liding Date: Wed, 31 Jul 2024 20:46:24 +0800 Subject: [PATCH] Add new processing rule and improve comments. --- .../article/article_body/ArticleBody.astro | 39 +++++++++++++------ 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/src/components/article/article_body/ArticleBody.astro b/src/components/article/article_body/ArticleBody.astro index 198183f..392ecc5 100644 --- a/src/components/article/article_body/ArticleBody.astro +++ b/src/components/article/article_body/ArticleBody.astro @@ -3,25 +3,42 @@ 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. +// 1. If a line belongs to the area where the line break should be kept, +// do not process this line. +// 2. If a line ends with [a-zA-Z], add a space to the end of this line. +// 3. If a line ends without [a-zA-Z] and its next line starts with [a-zA-Z], +// add a heading space to the next line. +// 4. If a line does not start in ASCII chars, join it to its 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; +// The first line of the HTML string is a table-of-content head recognized by +// remark-toc so it can be ignored. 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(/^(