Fix error handling of references with text

Previously the entire reference was treated into text, now we treat what
appears to be the target as text and turn the rest into a reference so
that what appears to be the text becomes the target.