拆分markdown
(segSharp, markdown string)
| 1002 | |
| 1003 | // 拆分markdown |
| 1004 | func SplitMarkdown(segSharp, markdown string) (markdowns []string) { |
| 1005 | var ( |
| 1006 | segIdx []int |
| 1007 | sharp = "#" |
| 1008 | sharpReplace = []string{ |
| 1009 | "#######", |
| 1010 | "######", |
| 1011 | "#####", |
| 1012 | "####", |
| 1013 | "###", |
| 1014 | "##", |
| 1015 | "#", |
| 1016 | } |
| 1017 | indexCodeBlockStart = -1 |
| 1018 | indexCodeBlockEnd = -1 |
| 1019 | ) |
| 1020 | |
| 1021 | replaceFmt := "\n${%v}$" |
| 1022 | for _, item := range sharpReplace { |
| 1023 | k := "\n" + item |
| 1024 | markdown = strings.Replace(markdown, k, fmt.Sprintf(replaceFmt, strings.Count(item, sharp)), -1) |
| 1025 | } |
| 1026 | |
| 1027 | seg := fmt.Sprintf("${%v}$", strings.Count(segSharp, sharp)) |
| 1028 | slice := strings.Split(markdown, "\n") |
| 1029 | |
| 1030 | for idx, item := range slice { |
| 1031 | item = strings.TrimSpace(item) |
| 1032 | if strings.Contains(item, "<pre") { |
| 1033 | indexCodeBlockStart = idx |
| 1034 | } |
| 1035 | |
| 1036 | if strings.Contains(item, "/pre>") { |
| 1037 | indexCodeBlockEnd = idx |
| 1038 | } |
| 1039 | |
| 1040 | if strings.Contains(item, "```") { |
| 1041 | if indexCodeBlockEnd >= indexCodeBlockStart { |
| 1042 | indexCodeBlockStart = idx |
| 1043 | } else { |
| 1044 | indexCodeBlockEnd = idx |
| 1045 | } |
| 1046 | } |
| 1047 | |
| 1048 | if idx > indexCodeBlockEnd && indexCodeBlockEnd >= indexCodeBlockStart && strings.HasPrefix(item, seg) { |
| 1049 | item = strings.Replace(item, seg, segSharp, -1) |
| 1050 | slice[idx] = item |
| 1051 | segIdx = append(segIdx, idx) |
| 1052 | } |
| 1053 | } |
| 1054 | |
| 1055 | recoverSharp := func(md string) string { |
| 1056 | for _, item := range sharpReplace { |
| 1057 | md = strings.Replace(md, fmt.Sprintf("${%v}$", strings.Count(item, sharp)), item, -1) |
| 1058 | } |
| 1059 | return md |
| 1060 | } |
| 1061 |
no test coverage detected