ChatGPT解决这个技术问题 Extra ChatGPT

Slack Markdown for Links Are Not Resolving

What am I doing wrong? None of the example Markdown links work so far in my Slack app.

I pasted in the example below in a chat in my Slack app. I got this example from slack markdown syntax and it still treats it as literal text in the Slack App:

[like this](http://someurl)

so I end up seeing that instead of just "like this" as a link in Slack chat.

Or maybe the above is wrong, in which case my question would then be how do you do explicitly create links in Slack? I want some text that I specify to be clickable to a specific URL (hyperlink).

Seems to be unresolved bug (doesn't work for me either) github.com/slackhq/hubot-slack/issues/114
not only the are not implementing this functionality, they also do not include a dark theme! this is outrageous for such a popular tool..on the other hand this is how popular tools become unpopular all of the sudden ...
Slack markdown link syntax is <http://someurl|like this> not the same as github. I think you are right that some of the Slack examples used to have that mistake in them, but github syntax does not work.

W
Wilhelm Klopp

Slack uses their own flavor of markdown:
Slack Markdown Links work in the following way: <http://someurl|like this>

Note: You can only do this via the Slack API and NOT just as a simple message you send via the Slack client.

https://i.stack.imgur.com/Bcpaj.png


is there a way to do this via the Slack client?
One way they could have avoided overcomplication would have been to stick with a popular and well-established format ... like Markdown. ;-)
+joeytwiddle That would mean Slack intentionally doing something that is user friendly and intuitive, which I'm pretty sure is against their company policy.
Please upvote this feature request here : slack.canny.io/feature-requests/p/…
Calling this a flavor of markdown is misleading. It's really more "their own formatting that sometimes looks a bit like markdown".
p
pillravi

Slack currently does not support hyperlinks; see this link which says:

Note: It’s not possible to hyperlink words in a Slack message.

UPDATE:

Slack has finally added this functionality to their chat interface (source):

Select text, then click the link icon in the formatting toolbar Select text, then press Cmd+Shift+U on Mac or Ctrl+Shift+U on Windows/Linux. Copy the link you'd like to share and paste it in the empty field under Link, then click Save.


The question is specifically about the slack-api which does support hyperlinked text.
This is unfortunate. Hyperlink has been around for more than 20 years, it's hard to assume this is a security issue. Pasting super long links is really annoying, at least slack should add an automatic URL shortening service (which would be optional of course).
It seems this is now possible. But I'm unable to get it to work. No matter what I do Slack just receives Array as the message in the channel. See the examples here: api.slack.com/messaging/composing/formatting#linking-urls
The original question asks about achieving this using markdown. This answer seems unrelated.
Worth mentioned that none of these work if you select Format messages with markup in the settings, to get rid of the text formatting toolbar.
l
lezhumain

This is not yet supported by Slack for direct messages, you can only do this using Slack API. But you can upvote the feature request I have submitted here.


Sorry, broken link :(
They seem to have disabled their canny channel to gather feature requests. Hope that is only temporary.
The link isn't broken. They intentionally disabled the board. Grr.
s
spottedmahn

As of today, 2020.02.14, in one of the Slack instances I'm a member in, I can create/paste hyperlinks! 🎉

https://i.stack.imgur.com/UZneU.png

Example pasting hyperlink from copy Teams meeting info into Slack:

https://i.stack.imgur.com/TbdNc.png

Slack has updated their documentation. Still no markdown way of doing it though 😢, i.e. [blah](https://stackoverflow.com) doesn't work.

Neat feature, select some text and CTRL + V when a URL is on the clipboard and it creates a hyperlink 🥳

https://i.stack.imgur.com/ynFT4.gif

Through some reverse engineering I was able to put text on the Windows Clipboard via C# and get it to paste into Slack:

var textToDisplay = "Test";
var url = "https://stackoverflow.com";
var arbitraryText = "Mike D.";
var dataObject = new DataObject();
//to my surprise, the Fragment comments ARE required
dataObject.SetData(DataFormats.Html, @$"<html><body>
    <!--StartFragment-->
    <a href=""{url}"">{textToDisplay}</a>
    <!--EndFragment-->
    </body></html>");
//have to set the Text format too otherwise it won't work
dataObject.SetData(DataFormats.Text, arbitraryText);
Clipboard.SetDataObject(dataObject);

Upvoted because this is essentially the answer. Love the paste-to-linkify, love the hotkey... unfortunately I have disabled their WYSIWYG so that it stops messing with other things I'm trying to write, and there doesn't seem to be any alternative for those of us happy using markdown(-ish) formatting in plain text.
@Tyler. I'm glad I'm not the only one that does this too!
I managed to make it work without the fragment comments
I rather hate the hotkey, Ctrl+Shift+U has been used for unicode character input on Linux (ibus input method) for decades
My slack has the link tool, but I'd really love to just do it with the markdown [text](url) like [their docs say should work[(slack.com/help/articles/202288908-Format-your-messages).
d
dududko

As of August 2021 this feature is finally enabled for markup mode in slack app. https://slack.com/intl/en-nl/help/articles/202288908-Format-your-messages#markup

Surround text with brackets, then surround the link with parentheses:
[your text](the link)

Confirmed that [label](link) does now work in the Slack app, but FYI this is no longer documented on the page referenced in this answer.
Thank you! You have made my day, month, year, and century.
but, at least on the mac app, this only works if you disable the WYSIWYG editor. unclear why they force seemingly unnecessary tradeoffs across either editor.
FYI, this still doesn't work in the API. Links still have to be in the older <L|T> format.
P
Prakhil TP

If you are using slack-bot or something that uses Slack API, you'll be able to use mrkdwn syntax for your messages.

<http://www.example.com|This message is a link>

Reference: https://api.slack.com/reference/surfaces/formatting


M
Michael Roberts

Reuben's answer will work, but it will look like an untitled file upload, as seen here: untitled file posted to Slack. With a slight modification, however, you can easily post a natural looking message featuring a working hyperlink such as you see here: natural looking Slack post with hyperlink by using the chat.postMessage Slack api method instead of "files.upload" and adopting Slack's own message formatting instead of Markdown. That would be done like this:

curl -F text="*<https://someurl|like this>*" -F as_user=true -F link_names=true -F channel=C1.....7L -F token=xoxp-... https://slack.com/api/chat.postMessage

The link_names=true argument isn't used in this example, but is useful to be able to @mention users and #refer to channels.


R
Reuben

As an alternative to Slack Messages (covered in Wilhem's answer), you can create Slack Posts via the API and use at least some Markdown. These both create <h2><a href="https://someurl">like this</a></h2>:

curl -F filetype=post -F content="# [like this](https://someurl)" -F channels=C1.....7L -F token=xoxp-... https://slack.com/api/files.upload

or swap content="..." for file=@post.md

curl -F filetype=post -F file=@post.md -F channels=C1.....7L -F token=xoxp-... https://slack.com/api/files.upload

This is using files.upload. I think the easiest way to try posting as yourself is with a legacy token. Get the channel ID from the URI of the channel.


T
Tzach Solomon

My markup setting was disabled, once I've enabled it (Preferences -> Advanced -> Format messages with markup) it worked.

https://i.stack.imgur.com/I5okL.png


It's not great in terms of usability that those are "exclusive".
r
ratbeard

I wrote this code to convert markdown links within a body of text into the link format slack expects:

      // Pretty hacky, convert [sup](http://foo.com) to <http://foo.com|sup>
      const reformatLinks = /\[(.*?)\]\((.*?)\)/g
      const slackBody = body.replace(reformatLinks, (_m, text, url) => `<${url}|${text}>`)

From what I can tell slack does not support image links inside text.


K
Kev

None of the markup in the other answers here (Markdown, <a|b>, etc.) works anymore.

If you're composing by hand, there's something that to me is better than the keyboard shortcut in the docs (since that shortcut conflicts with global defaults on Linux and is a pain to work around).

Copy your URL to the clipboard, then select the text you'd like to be the anchor text, and just paste the URL as if you were going to replace the selected text with the URL. Slack instead automatically turns the selected text into a link.


This is the only solution for now. Markdown for some stuff works in slack, but links don't. Thx Kev.
l
lukasell

Had a problem with this form of markdown when the url is involving a vertical bar | solved this with urltext.replace("|", "%7C")


D
Daniel Firsht

Following spottedmahn's answer, here's how I got it working in Javascript. Note that for Firefox, the user needs to enable a flag for this to work.

a.addEventListener("click", function() {
    var textToDisplay = "foo";
    var url = "https://stackoverflow.com";
    var message = `<html><body>
    <!--StartFragment-->
    <a href="${url}">${textToDisplay}</a>
    <!--EndFragment-->
    </body></html>`;
    const htmlBlob = new Blob([message], {type : 'text/html'});
    const textBlob = new Blob(["sda"], {type : 'text/plain'});

    const cbi = new ClipboardItem({
        ['text/html']: htmlBlob,
        ['text/plain']: textBlob
    });
    navigator.clipboard.write([cbi]);
});

N
NoobishSRE

Super late to the party but I recently discovered that you can use a URL shortener to create a https link which you can then reference using markdown links.