util.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. import type {
  2. MarkdownObject
  3. } from "./type";
  4. import {
  5. DEFAULT_VARIANTS
  6. } from "./constant";
  7. export const imageMarkdown = ({
  8. index,
  9. line
  10. }: {
  11. index: number;
  12. line: string;
  13. }): MarkdownObject => {
  14. const contentMatch = line.match(/\[(.*?)\]/);
  15. const imageContent = (contentMatch && contentMatch[1]) ? contentMatch[1] : "";
  16. const tmpLine = line.replace(/\[.*?\]/g, "");
  17. const urlMatch = tmpLine.match(/\((.*?)\)/);
  18. const imageURL = (urlMatch && urlMatch[1]) ? urlMatch[1] : "";
  19. const tmpLine2 = tmpLine.replace(/\((.*?)\)/, "");
  20. const imageSizeMatch = tmpLine2.match(/<(.*?)>/);
  21. const imageSize = (imageSizeMatch && imageSizeMatch[1]) ? imageSizeMatch[1].split("x").map(e => {
  22. return e.indexOf("%") !== -1 || e.indexOf("auto") !== -1 ? e : Number(e);
  23. }) : undefined;
  24. const tmpLine3 = tmpLine2.replace(/<(.*?)>/, "");
  25. const resizeModeMatch = tmpLine3.match(/--(.*?)--/);
  26. const resizeMode = (resizeModeMatch && resizeModeMatch[1]) ? resizeModeMatch[1] as "cover" | "contain" | "center" | "none" | "repeat" | "stretch" : undefined;
  27. return {
  28. resizeMode: resizeMode,
  29. content: imageContent,
  30. nativeType: "image",
  31. size: imageSize,
  32. type: "image",
  33. url: imageURL,
  34. key: index
  35. };
  36. };
  37. export const textMarkdown = ({
  38. index,
  39. type,
  40. line
  41. }: {
  42. type: "# " | "## " | "### " | "#### " | "##### " | "###### " | "<p>";
  43. index: number;
  44. line: string;
  45. }): MarkdownObject => {
  46. return {
  47. content: line.replace(type, ""),
  48. variant: DEFAULT_VARIANTS[type],
  49. nativeType: type,
  50. type: "text",
  51. key: index
  52. };
  53. };
  54. export const enterMarkdown = ({
  55. index,
  56. line
  57. }: {
  58. index: number;
  59. line: string;
  60. }): MarkdownObject => {
  61. const _line = line.replace("<br/>", "");
  62. return {
  63. variant: DEFAULT_VARIANTS["<br/>"],
  64. nativeType: "<br/>",
  65. content: _line,
  66. type: "space",
  67. key: index
  68. };
  69. };
  70. export const codeMarkdown = ({
  71. skippedIndices,
  72. index,
  73. lines
  74. }: {
  75. skippedIndices: number[];
  76. lines: string[];
  77. index: number;
  78. }): MarkdownObject | void => {
  79. let endIndex = -1;
  80. for (let j = index + 1; j < lines.length; j++) {
  81. if (lines[j]?.startsWith("```")) {
  82. endIndex = j;
  83. break;
  84. }
  85. }
  86. if (endIndex !== -1) {
  87. for (let k = index + 1; k <= endIndex; k++) {
  88. skippedIndices.push(k);
  89. }
  90. const codeContent = lines.slice(index + 1, endIndex).join("\n");
  91. return {
  92. variant: DEFAULT_VARIANTS["```"],
  93. content: codeContent,
  94. nativeType: "```",
  95. type: "code",
  96. key: index
  97. };
  98. }
  99. };
  100. export const centerMarkdown = ({
  101. skippedIndices,
  102. index,
  103. lines
  104. }: {
  105. skippedIndices: number[];
  106. lines: string[];
  107. index: number;
  108. }): MarkdownObject | void => {
  109. let endIndex = -1;
  110. for (let j = index + 1; j < lines.length; j++) {
  111. if (lines[j]?.startsWith("</center>")) {
  112. endIndex = j;
  113. break;
  114. }
  115. }
  116. if (endIndex !== -1) {
  117. for (let k = index + 1; k <= endIndex; k++) {
  118. skippedIndices.push(k);
  119. }
  120. const centerContent = lines.slice(index + 1, endIndex).join("\n");
  121. return {
  122. content: centerContent,
  123. nativeType: "center",
  124. type: "center",
  125. key: index
  126. };
  127. }
  128. };
  129. export const leftMarkdown = ({
  130. skippedIndices,
  131. index,
  132. lines
  133. }: {
  134. skippedIndices: number[];
  135. lines: string[];
  136. index: number;
  137. }): MarkdownObject | void => {
  138. let endIndex = -1;
  139. for (let j = index + 1; j < lines.length; j++) {
  140. if (lines[j]?.startsWith("</left>")) {
  141. endIndex = j;
  142. break;
  143. }
  144. }
  145. if (endIndex !== -1) {
  146. for (let k = index + 1; k <= endIndex; k++) {
  147. skippedIndices.push(k);
  148. }
  149. const leftContent = lines.slice(index + 1, endIndex).join("\n");
  150. return {
  151. content: leftContent,
  152. nativeType: "left",
  153. type: "left",
  154. key: index
  155. };
  156. }
  157. };
  158. export const rightMarkdown = ({
  159. skippedIndices,
  160. index,
  161. lines
  162. }: {
  163. skippedIndices: number[];
  164. lines: string[];
  165. index: number;
  166. }): MarkdownObject | void => {
  167. let endIndex = -1;
  168. for (let j = index + 1; j < lines.length; j++) {
  169. if (lines[j]?.startsWith("</right>")) {
  170. endIndex = j;
  171. break;
  172. }
  173. }
  174. if (endIndex !== -1) {
  175. for (let k = index + 1; k <= endIndex; k++) {
  176. skippedIndices.push(k);
  177. }
  178. const rightContent = lines.slice(index + 1, endIndex).join("\n");
  179. return {
  180. content: rightContent,
  181. nativeType: "right",
  182. type: "right",
  183. key: index
  184. };
  185. }
  186. };
  187. export const linkMarkdown = ({
  188. skippedIndices,
  189. linkStartRegex,
  190. index,
  191. lines,
  192. line
  193. }: {
  194. skippedIndices: number[];
  195. linkStartRegex: RegExp;
  196. lines: string[];
  197. index: number;
  198. line: string;
  199. }): MarkdownObject | void => {
  200. let endIndex = -1;
  201. for (let j = index + 1; j < lines.length; j++) {
  202. if (lines[j]?.startsWith("</link>")) {
  203. endIndex = j;
  204. break;
  205. }
  206. }
  207. if (endIndex !== -1) {
  208. const match = line.match(linkStartRegex);
  209. const href = match ? match[1] : "";
  210. for (let k = index + 1; k <= endIndex; k++) {
  211. skippedIndices.push(k);
  212. }
  213. const linkContent = lines.slice(index + 1, endIndex).join("\n");
  214. return {
  215. content: linkContent,
  216. nativeType: "link",
  217. type: "link",
  218. key: index,
  219. url: href
  220. };
  221. }
  222. };
  223. export const blockquoteMarkdown = ({
  224. skippedIndices,
  225. index,
  226. lines
  227. }: {
  228. skippedIndices: number[];
  229. lines: string[];
  230. index: number;
  231. }): MarkdownObject | void => {
  232. let endIndex = -1;
  233. for (let j = index + 1; j < lines.length; j++) {
  234. if (lines[j]?.startsWith(">>")) {
  235. endIndex = j;
  236. break;
  237. }
  238. }
  239. if (endIndex !== -1) {
  240. for (let k = index + 1; k <= endIndex; k++) {
  241. skippedIndices.push(k);
  242. }
  243. const quoteContent = lines.slice(index + 1, endIndex).join("\n");
  244. return {
  245. variant: DEFAULT_VARIANTS["<<"],
  246. content: quoteContent,
  247. type: "blockquote",
  248. nativeType: "<<",
  249. key: index
  250. };
  251. }
  252. };
  253. export const listMarkdown = ({
  254. isStarList,
  255. index,
  256. line
  257. }: {
  258. isStarList: boolean;
  259. index: number;
  260. line: string;
  261. }): MarkdownObject => {
  262. return {
  263. variant: DEFAULT_VARIANTS[isStarList ? "* " : "- "],
  264. nativeType: isStarList ? "* " : "- ",
  265. content: line.substring(2),
  266. type: "list",
  267. key: index
  268. };
  269. };
  270. export const parseMarkdown = (rawText: string): Array<MarkdownObject> => {
  271. const lines = rawText.split("\n");
  272. const skippedIndices: number[] = [];
  273. return lines.map((line, index) => {
  274. if (skippedIndices.includes(index)) {
  275. return {
  276. type: undefined,
  277. nativeType: "",
  278. content: "",
  279. key: index
  280. };
  281. }
  282. if (line.startsWith("# ")) return textMarkdown({
  283. type: "# ",
  284. index,
  285. line
  286. });
  287. if (line.startsWith("## ")) return textMarkdown({
  288. type: "## ",
  289. index,
  290. line
  291. });
  292. if (line.startsWith("### ")) return textMarkdown({
  293. type: "### ",
  294. index,
  295. line
  296. });
  297. if (line.startsWith("#### ")) return textMarkdown({
  298. type: "#### ",
  299. index,
  300. line
  301. });
  302. if (line.startsWith("##### ")) return textMarkdown({
  303. type: "##### ",
  304. index,
  305. line
  306. });
  307. if (line.startsWith("###### ")) return textMarkdown({
  308. type: "###### ",
  309. index,
  310. line
  311. });
  312. if (line.startsWith("<br/>") || line === "") return enterMarkdown({
  313. index,
  314. line
  315. });
  316. if (line.startsWith("```")) {
  317. const returnedData = codeMarkdown({
  318. skippedIndices,
  319. index,
  320. lines
  321. });
  322. if(returnedData) return returnedData;
  323. }
  324. if (line.startsWith("<center>")) {
  325. const returnedData = centerMarkdown({
  326. skippedIndices,
  327. index,
  328. lines
  329. });
  330. if(returnedData) return returnedData;
  331. }
  332. if (line.startsWith("<right>")) {
  333. const returnedData = rightMarkdown({
  334. skippedIndices,
  335. index,
  336. lines
  337. });
  338. if(returnedData) return returnedData;
  339. }
  340. if (line.startsWith("<left>")) {
  341. const returnedData = leftMarkdown({
  342. skippedIndices,
  343. index,
  344. lines
  345. });
  346. if(returnedData) return returnedData;
  347. }
  348. const linkStartRegex = /^<link\s+href=["'](.*?)["']>/;
  349. if (linkStartRegex.test(line)) {
  350. const returnedData = linkMarkdown({
  351. skippedIndices,
  352. linkStartRegex,
  353. index,
  354. lines,
  355. line
  356. });
  357. if(returnedData) return returnedData;
  358. }
  359. if (line.startsWith("<<")) {
  360. const returnedData = blockquoteMarkdown({
  361. skippedIndices,
  362. index,
  363. lines
  364. });
  365. if(returnedData) return returnedData;
  366. }
  367. const isStartWithImage = line.startsWith("[");
  368. if(isStartWithImage || line.startsWith("[")) return imageMarkdown({
  369. index,
  370. line
  371. });
  372. const isStarList = line.startsWith("* ");
  373. if (isStarList || line.startsWith("- ")) return listMarkdown({
  374. isStarList,
  375. index,
  376. line
  377. });
  378. return {
  379. variant: DEFAULT_VARIANTS["<p>"],
  380. nativeType: "<p>",
  381. content: line,
  382. type: "text",
  383. key: index
  384. };
  385. });
  386. };