53 lines
1.4 KiB
JavaScript
53 lines
1.4 KiB
JavaScript
const markdownHeading = /^(#{1,6})\s+(.+)$/;
|
|
const orderedListItem = /^(\d+)[.、]\s*(.+)$/;
|
|
const unorderedListItem = /^[-*]\s+(.+)$/;
|
|
|
|
export const formatComplianceContent = (value, documentTitle = "") => {
|
|
if (typeof value !== "string" || !value.trim()) return [];
|
|
|
|
const blocks = value
|
|
.replace(/\r\n?/g, "\n")
|
|
.split("\n")
|
|
.map((line) => line.trim())
|
|
.filter(Boolean)
|
|
.map((line) => {
|
|
const heading = line.match(markdownHeading);
|
|
if (heading) {
|
|
return {
|
|
type: heading[1].length === 1 ? "title" : "heading",
|
|
text: heading[2].trim(),
|
|
};
|
|
}
|
|
|
|
const ordered = line.match(orderedListItem);
|
|
if (ordered) {
|
|
return {
|
|
type: "list-item",
|
|
marker: `${ordered[1]}.`,
|
|
text: ordered[2].trim(),
|
|
};
|
|
}
|
|
|
|
const unordered = line.match(unorderedListItem);
|
|
if (unordered) {
|
|
return { type: "list-item", marker: "•", text: unordered[1].trim() };
|
|
}
|
|
|
|
return { type: "paragraph", text: line };
|
|
});
|
|
|
|
const duplicateTitle =
|
|
blocks[0]?.type === "title" &&
|
|
blocks[0].text === String(documentTitle || "").trim();
|
|
if (!duplicateTitle) return blocks;
|
|
|
|
let contentBlocks = blocks.slice(1);
|
|
while (
|
|
contentBlocks[0]?.type === "paragraph" &&
|
|
/^(?:版本号|版本|发布日期|生效日期)\s*[::]/.test(contentBlocks[0].text)
|
|
) {
|
|
contentBlocks = contentBlocks.slice(1);
|
|
}
|
|
return contentBlocks;
|
|
};
|