| 1805 | |
| 1806 | // Attempt to parse a link reference, modifying refmap. |
| 1807 | var parseReference = function(s, refmap) { |
| 1808 | this.subject = s; |
| 1809 | this.pos = 0; |
| 1810 | var rawlabel; |
| 1811 | var dest; |
| 1812 | var title; |
| 1813 | var matchChars; |
| 1814 | var startpos = this.pos; |
| 1815 | |
| 1816 | // label: |
| 1817 | matchChars = this.parseLinkLabel(); |
| 1818 | if (matchChars === 0) { |
| 1819 | return 0; |
| 1820 | } else { |
| 1821 | rawlabel = this.subject.substr(0, matchChars); |
| 1822 | } |
| 1823 | |
| 1824 | // colon: |
| 1825 | if (this.peek() === C_COLON) { |
| 1826 | this.pos++; |
| 1827 | } else { |
| 1828 | this.pos = startpos; |
| 1829 | return 0; |
| 1830 | } |
| 1831 | |
| 1832 | // link url |
| 1833 | this.spnl(); |
| 1834 | |
| 1835 | dest = this.parseLinkDestination(); |
| 1836 | if (dest === null || dest.length === 0) { |
| 1837 | this.pos = startpos; |
| 1838 | return 0; |
| 1839 | } |
| 1840 | |
| 1841 | var beforetitle = this.pos; |
| 1842 | this.spnl(); |
| 1843 | title = this.parseLinkTitle(); |
| 1844 | if (title === null) { |
| 1845 | title = ''; |
| 1846 | // rewind before spaces |
| 1847 | this.pos = beforetitle; |
| 1848 | } |
| 1849 | |
| 1850 | // make sure we're at line end: |
| 1851 | var atLineEnd = true; |
| 1852 | if (this.match(reSpaceAtEndOfLine) === null) { |
| 1853 | if (title === '') { |
| 1854 | atLineEnd = false; |
| 1855 | } else { |
| 1856 | // the potential title we found is not at the line end, |
| 1857 | // but it could still be a legal link reference if we |
| 1858 | // discard the title |
| 1859 | title = ''; |
| 1860 | // rewind before spaces |
| 1861 | this.pos = beforetitle; |
| 1862 | // and instead check if the link URL is at the line end |
| 1863 | atLineEnd = this.match(reSpaceAtEndOfLine) !== null; |
| 1864 | } |