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)
| 729 | // wins, and the dropped one's argument is reported unwritten so the caller |
| 730 | // appends it. |
| 731 | func applyEdits(md string, edits []edit) (string, map[int]bool) { |
| 732 | written := map[int]bool{} |
| 733 | if len(edits) == 0 { |
| 734 | return md, written |
| 735 | } |
| 736 | |
| 737 | sort.SliceStable(edits, func(i, j int) bool { |
| 738 | if edits[i].at.start != edits[j].at.start { |
| 739 | return edits[i].at.start < edits[j].at.start |
| 740 | } |
| 741 | return edits[i].at.stop > edits[j].at.stop |
| 742 | }) |
| 743 | |
| 744 | var kept []edit |
| 745 | end := 0 |
| 746 | for _, e := range edits { |
| 747 | if e.at.start < end || e.at.start > e.at.stop || e.at.stop > len(md) { |
| 748 | continue |
| 749 | } |
| 750 | kept = append(kept, e) |
| 751 | written[e.a] = true |
| 752 | end = e.at.stop |
| 753 | } |
| 754 | |
| 755 | out := md |
| 756 | for _, e := range slices.Backward(kept) { |
| 757 | out = out[:e.at.start] + e.text + out[e.at.stop:] |
| 758 | } |
| 759 | return out, written |
| 760 | } |
| 761 | |
| 762 | // standsAlone reports whether replacing a node with a bare URL will render as |
| 763 | // a player. |
no outgoing calls
no test coverage detected