HubSpot/draft-convert

URL mutated in case URI missing

priyankshivhare opened this issue · 1 comments

Our implementation for converting Entity to HTML is like this

const entityToHTML = (entity, originalText) => {
    if (entity.type === LINK_ENTITY_TYPE) {
        return <a href={entity.data.url}>{originalText}</a>;
    }
    if (entity.type === PLACEHOLDER_ENTITY_TYPE) {
        return entity.data.dataKey;
    }
    return originalText;
};

Problem is that if a link doesn't contain proper URI like http:// or https:// relative URL from the browser is appended in the beginning and the link gets mutated

const htmlToEntityHelper = (nodeName, node, createEntity) => {
 if (nodeName === 'a') {
    return createEntity('LINK', 'MUTABLE', { url: node.getAttribute('href'), target: node.target });
  }
};
 const contentState = convertFromHTML({
    htmlToEntity: (nodeName, node, createEntity) => htmlToEntityHelper(nodeName, node, createEntity),
  })(html);

the issue is that in the docs in the entity helper, they have { url: node.href } which results in a different behavior than { url: node.getAttribute('href') }