applyEdits rewrites the markdown back to front, so that an earlier edit cannot shift a later one, and reports which arguments were actually written. Edits overlap when a video embed alone in its paragraph is replaced by a bare URL and its alt text carries a reference of its own. The wider edit wins
(md string, edits []edit)
| 766 | // wins, and the dropped one's argument is reported unwritten so the caller |
| 767 | // appends it. |
| 768 | func applyEdits(md string, edits []edit) (string, map[int]bool) { |
| 769 | written := map[int]bool{} |
| 770 | if len(edits) == 0 { |
| 771 | return md, written |
| 772 | } |
| 773 | |
| 774 | sort.SliceStable(edits, func(i, j int) bool { |
| 775 | if edits[i].at.start != edits[j].at.start { |
| 776 | return edits[i].at.start < edits[j].at.start |
| 777 | } |
| 778 | return edits[i].at.stop > edits[j].at.stop |
| 779 | }) |
| 780 | |
| 781 | var kept []edit |
| 782 | end := 0 |
| 783 | for _, e := range edits { |
| 784 | if e.at.start < end || e.at.start > e.at.stop || e.at.stop > len(md) { |
| 785 | continue |
| 786 | } |
| 787 | kept = append(kept, e) |
| 788 | written[e.a] = true |
| 789 | end = e.at.stop |
| 790 | } |
| 791 | |
| 792 | out := md |
| 793 | for _, e := range slices.Backward(kept) { |
| 794 | out = out[:e.at.start] + e.text + out[e.at.stop:] |
| 795 | } |
| 796 | return out, written |
| 797 | } |
| 798 | |
| 799 | // standsAlone reports whether replacing a node with a bare URL will render as |
| 800 | // a player. |
no outgoing calls
no test coverage detected