| ... | @@ -1498,6 +1498,22 @@ | ... | @@ -1498,6 +1498,22 @@ |
| 1498 | } | 1498 | } |
| 1499 | ]; | 1499 | ]; |
| 1500 | | 1500 | |
| | 1501 | // Links, images and inner links don't use the same marker to wrap their content. |
| | 1502 | const linksFormat = [ |
| | 1503 | { |
| | 1504 | prefix: "[", |
| | 1505 | regex: /\[([^\]]*)\]\(([^\)]*)\)/, |
| | 1506 | urlIndex: 2, // Index in the match that contains the link URL |
| | 1507 | textIndex: 1 // Index in the match that contains the link text |
| | 1508 | }, |
| | 1509 | { |
| | 1510 | prefix: "h", |
| | 1511 | regex: /http[s]?:\/\/[^\s]+/, |
| | 1512 | urlIndex: 0, |
| | 1513 | textIndex: 0 |
| | 1514 | } |
| | 1515 | ]; |
| | 1516 | |
| 1501 | const stack = []; | 1517 | const stack = []; |
| 1502 | | 1518 | |
| 1503 | var innerHTML = ""; | 1519 | var innerHTML = ""; |
| ... | @@ -1548,6 +1564,29 @@ | ... | @@ -1548,6 +1564,29 @@ |
| 1548 | currentRun += innerText[i]; | 1564 | currentRun += innerText[i]; |
| 1549 | in_code = true; | 1565 | in_code = true; |
| 1550 | } else { | 1566 | } else { |
| | 1567 | var foundMatches = false; |
| | 1568 | |
| | 1569 | for (var j = 0; j < linksFormat.length; j++) { |
| | 1570 | const linkFmt = linksFormat[j]; |
| | 1571 | |
| | 1572 | if (linkFmt.prefix == innerText[i]) { |
| | 1573 | var remaining = innerText.substring(i); |
| | 1574 | var matches = remaining.match(linkFmt.regex); |
| | 1575 | |
| | 1576 | if (matches) { |
| | 1577 | flushRun(); |
| | 1578 | innerHTML += ' <a href="' + matches[linkFmt.urlIndex] + '">' + matches[linkFmt.textIndex] + '</a> '; |
| | 1579 | i += matches[0].length; // Skip the fragment we just consumed |
| | 1580 | foundMatches = true; |
| | 1581 | break; |
| | 1582 | } |
| | 1583 | } |
| | 1584 | } |
| | 1585 | |
| | 1586 | if (foundMatches) { |
| | 1587 | continue; |
| | 1588 | } |
| | 1589 | |
| 1551 | var any = false; | 1590 | var any = false; |
| 1552 | for (var idx = (stack.length > 0 ? -1 : 0); idx < formats.length; idx++) { | 1591 | for (var idx = (stack.length > 0 ? -1 : 0); idx < formats.length; idx++) { |
| 1553 | const fmt = idx >= 0 ? formats[idx] : stack[stack.length - 1]; | 1592 | const fmt = idx >= 0 ? formats[idx] : stack[stack.length - 1]; |