Skip to content

Commit

Permalink
feat(docs): added list handle
Browse files Browse the repository at this point in the history
  • Loading branch information
TrofimovAnton85 committed Oct 2, 2024
1 parent b7b3921 commit 59ad386
Showing 1 changed file with 33 additions and 28 deletions.
61 changes: 33 additions & 28 deletions src/components/ParserOpenRPC/DetailsBox/MDContent.tsx
Original file line number Diff line number Diff line change
@@ -1,43 +1,48 @@
import React from "react";

const parseLists = (content: string) => {
const lines = content.split('\n');
const parseLists = (text: string) => {
const lines = text.split('\n');
let result = '';
let isFirstLevelOpen = false;
let isSecondLevelOpen = false;
let inList = false;
let inSubList = false;

lines.forEach((line) => {
if (line.match(/^ {2}-\s+/)) {
if (!isSecondLevelOpen) {
result += '<ul>';
isSecondLevelOpen = true;
}
result += `<li>${line.trim().substring(4)}</li>`;
} else if (line.match(/^ -\s+/)) {
if (isSecondLevelOpen) {
result += '</ul>';
isSecondLevelOpen = false;
const trimmed = line.trim();
const isListItem = trimmed.startsWith('- ');
const isSubListItem = line.startsWith(' - ');

if (isListItem && !isSubListItem) {
if (!inList) {
result += '<ul>\n';
inList = true;
} else if (inSubList) {
result += '</ul>\n';
inSubList = false;
}
if (!isFirstLevelOpen) {
result += '<ul>';
isFirstLevelOpen = true;
result += `<li>${trimmed.slice(2).trim()}</li>\n`;
} else if (isSubListItem) {
if (!inSubList) {
result = result.replace(/<\/li>\n$/, '');
result += '<ul>\n';
inSubList = true;
}
result += `<li>${line.trim().substring(2)}</li>`;
result += `<li>${trimmed.slice(4).trim()}</li>\n`;
} else {
if (isSecondLevelOpen) {
result += '</ul>';
isSecondLevelOpen = false;
if (inSubList) {
result += '</ul>\n';
inSubList = false;
}
if (isFirstLevelOpen) {
result += '</ul>';
isFirstLevelOpen = false;
if (inList) {
result += '</ul>\n';
inList = false;
}
result += line;
result += `${line}\n`;
}
});
if (isSecondLevelOpen) result += '</ul>';
if (isFirstLevelOpen) result += '</ul>';
if (inSubList) result += '</ul>\n';
if (inList) result += '</ul>\n';
return result;
};
}

const parseMarkdown = (content: string) => {
return parseLists(
Expand Down

0 comments on commit 59ad386

Please sign in to comment.