How to hide url link in edit mode until hovered on?

Over the years there were several related topics but solutions from them no longer work:
How to make the url link shorter in edit mode? - Help - Obsidian Forum
Hide or Truncate URLs in Editor using CSS? (one year later) - Help - Obsidian Forum
Hide or Truncate URLs in Editor using CSS? - Help - Obsidian Forum

Problem:

Some links are very long and when they expand they are very distracting.
Often I don’t need to see the exact link and want to only edit the title of the link because I’m used to editors like workflowy (mentioned here How to make the url link shorter in edit mode?).

Why current default editor’s behaviour doesn’t help:

Current behaviour (Version 1.5.12) shows only link title and shows URL only if cursor is on title.
Title can be very long and if I have multiple links on each line moving cursor vertically can be distracting as they expand.

Solution:

CSS snippet that replaces link URL with a emoji and shows it only if you hove mouse over it.

This also perfectly works on iOS devices. (Tap next to link to put cursor, drag cursor to the link, then tap on emoji to show URL)

This forum is very confusing, I couldn’t publish edits to my post and I couldn’t delete the post, so here is the end of the post:

You will need to install CSS snippet below.
(Instructions for installing snippet are provided in this video: https://youtu.be/UM7HH_sGRxs)

CSS code:

/* Hide the URL text and show the symbol */
div.cm-line .cm-string.cm-url:not(.cm-formatting) {
    font-size: 0;
}

/* Display a symbol after the URL */
div.cm-line .cm-string.cm-url:not(.cm-formatting)::after {
    content: '🔗'; /* Replace with your desired symbol */
    font-size: 1rem; /* Adjust font size as needed */
    color: inherit; /* Inherit color from the parent element */
}

/* Ensure the URL text is visible when the cursor is over it */
div.cm-line .cm-string.cm-url:not(.cm-formatting):hover {
    font-size: inherit;
}

/* Hide the symbol when the cursor is over the URL */
div.cm-line .cm-string.cm-url:not(.cm-formatting):hover::after {
    content: '';
}

I’m not very savvy about WEB development, but managed to make it with GPT, here is the example how you can do it:
https://chatgpt.com/share/08b68648-4fab-4017-9d2c-c3011050d738

DEMO:
Screen Recording 2024-06-01 at 20.29.11

3 Likes

I literally just made an account on this forum to thank you, because this works wonderfully — and I couldn’t find this anywhere else.

1 Like

Thank you! Glad that it helped you too)

I have been looking forward to this feature for a long time, and finally someone has brought it up. Thank you very much for sharing!

1 Like

Hi! I made an optimized version of this css:


/* Moy Link Optimize.css */
/* 相关帖子:https://forum-zh.obsidian.md/t/topic/38000/6 */

/* 点击链接的时候不跳转 */
/* Style Settings 开关 */
/* @settings

name: Moy Link Mods
id: moy-link-mods
settings:
    - 
        id: link-editing-mode
        title: Link Editing Mode
        title.zh: 链接编辑模式
        description: Cancel the link left mouse button click event
        description.zh: 是否取消链接的左键点击功能
        type: class-toggle
        default: true
        addCommand: true
    - 
        id: link-shorten
        title: Link Shorten
        title.zh: 缩短链接
        description: Shorten the link, unless mouse hover
        description.zh: 将链接缩短为 emoji,鼠标经过才完整显示
        type: class-toggle
        default: true
        addCommand: true

*/


.link-editing-mode .cm-link .cm-underline:not(:hover),
.link-editing-mode .cm-hmd-internal-link .cm-underline {
  pointer-events: none;
}


/* 隐藏过长的链接网址部分 */
/* Src: https://forum.obsidian.md/t/how-to-hide-url-link-in-edit-mode-until-hovered-on/82827 */
/* Hide the URL text and show the symbol */
.link-shorten div.cm-line .cm-string.cm-url:not(.cm-formatting) {
    font-size: 0;
    transition: font-size .2s cubic-bezier(1,0,.9,1) !important; 
    transition-delay: 0.3s !important;
    &::before {
        content: '🔗';
    }
}

/* Display a symbol after the URL */
.link-shorten div.cm-line .cm-string.cm-url:not(.cm-formatting)::before {
    content: '🔗'; /* Replace with your desired symbol */
    font-size: 1rem; /* Adjust font size as needed */
    color: inherit; /* Inherit color from the parent element */
/*    font-size: var(--font-adaptive-normal);*/
}

/* Ensure the URL text is visible when the cursor is over it */
.link-shorten div.cm-line .cm-string.cm-url:not(.cm-formatting):hover {
    font-size: inherit;
    transition: font-size 0.2s !important;
/*  延迟一会儿再出现   */
    transition-delay: 300ms !important; 
}

/* Hide the symbol when the cursor is over the URL */
.link-shorten div.cm-line .cm-string.cm-url:not(.cm-formatting):hover::before {
    /* content: ''; */
}


/* 修改 wikilink 格式的 */
/* Modified by Moy */
.link-shorten .cm-hmd-internal-link.cm-link-has-alias {
    font-size: 0;
    &:not(:hover){
        transition: font-size .2s cubic-bezier(1,0,.9,1) !important; 
        transition-delay: 0.3s !important;
    }
    &:hover {
        font-size: inherit;
        transition: font-size 0.2s !important;
        transition-delay: 200ms !important;
    }
}

/*.link-shorten .cm-hmd-internal-link.cm-link-has-alias:hover {
    font-size: inherit;
    transition: font-size 0.1s !important; 
}*/

.link-shorten .cm-hmd-internal-link.cm-link-has-alias:not(.cm-formatting)::before {
    content: '📜'; /* Replace with your desired symbol 📄 */
    font-size: 1rem; /* Adjust font size as needed */
    color: inherit; /* Inherit color from the parent element */
/*    font-size: var(--font-adaptive-normal);*/
}
  1. Added delay time and transition, so that the text wouldn’t suddenly show or disappear
    95564f8f-d315-4e00-91da-6608a47aa1fa

  2. Supportted the [[wikilink]] with Display text, as well

  3. Added the Style Settings support, so that you can switch it on/off with command or in the setting:

The modification is also based on another thread in CN forum: 【实用技巧】如何改善笔记的链接编辑体验 - 经验分享 - Obsidian 中文论坛

  1. Added another feature: with the Link Editing Mode, the click event of link would be blocked, so that we can easily edit the link text
    bfbeca63-fe90-4438-a0f3-cdd110b70cc0
2 Likes

Wow, cool! I like the idea with hiding long note name and leaving only alias!
Thanks for sharing!

1 Like

Thank you so much. 太感谢了!

1 Like

You’re welcome 不客气 ; )