Skip to content

Commit

Permalink
Merge pull request #555 from NASA-IMPACT/fix/links
Browse files Browse the repository at this point in the history
Fix links
  • Loading branch information
vgeorge authored Oct 6, 2023
2 parents c334d3c + 7748049 commit d16eb5a
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion app/assets/scripts/components/slate/plugins/link/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,25 @@ export const LinkPlugin = {
onUse: onLinkUse
};

/**
* Set the http protocol if it is missing.
* @param {String} link The link to check.
* @returns {String} The link with http protocol.
*/
function setHttp(link) {
if (!link) {
return link;
}

let newLink = link;

if (!link.startsWith('http://') && !link.startsWith('https://')) {
newLink = `https://${link}`;
}

return newLink;
}

export const onLinkEditorAction = (editor, action, payload) => {
switch (action) {
case 'cancel':
Expand All @@ -133,7 +152,7 @@ export const onLinkEditorAction = (editor, action, payload) => {
// Reselect value.
Transforms.select(editor, editor.linkEditor.getData().selection);
// Upsert the link.
upsertLinkAtSelection(editor, payload.value, { wrap: true });
upsertLinkAtSelection(editor, setHttp(payload.value), { wrap: true });
// Refocus the editor.
ReactEditor.focus(editor);
// Reset the link editor to hide it.
Expand Down

0 comments on commit d16eb5a

Please sign in to comment.