Skip to content

Commit

Permalink
Only use regexes supported by Safari (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
rexgarland authored Aug 7, 2023
1 parent 19a2290 commit 05842b5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
8 changes: 8 additions & 0 deletions __test__/slackify-markdown.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const fsPromises = require('fs/promises');

const slackifyMarkdown = require('..');

const zws = String.fromCharCode(0x200B); // zero-width-space
Expand Down Expand Up @@ -242,3 +244,9 @@ test('User mention', () => {

expect(slackifyMarkdown(mrkdown)).toBe(slack);
});

test('code does not use negative lookarounds', async () => {
const sourceCode = await fsPromises.readFile('./src/slackify.js', 'utf8');
const usesNegativeLookaround = /\/\(\?<?!/;
expect(sourceCode).not.toMatch(usesNegativeLookaround);
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "slackify-markdown",
"version": "4.3.1",
"version": "4.3.2",
"description": "Convert markdown into Slack-specific markdown",
"license": "MIT",
"main": "index.js",
Expand Down
19 changes: 15 additions & 4 deletions src/slackify.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@ const { wrap, isURL, isPotentiallyEncoded } = require('./utils');
// fixes slack in-word formatting (e.g. hel*l*o)
const zeroWidthSpace = String.fromCharCode(0x200B);

const escapeSpecials = text => {
const escaped = text
.replace('&', '&amp;')

This comment has been minimized.

Copy link
@christophehurpeau

christophehurpeau Oct 22, 2023

Contributor

replace only replaces the first occurence, is that expected ?
before, it was /&/g, '&amp;', /g replaces all occurences
image

.replace(/<([^@]|$)/g, (_, m) => `&lt;${m}`)
.replace(/^(.*)>/g, (_, m) => {
const isEndOfUserMention = Boolean(m.match(/<@[A-Z0-9]+$/));
if (isEndOfUserMention) {
return `${m}>`;
}
return `${m}&gt;`;
});
return escaped;
};

/**
* Creates custom `mdast-util-to-markdown` handlers that tailor the output for
* Slack Markdown.
Expand Down Expand Up @@ -120,10 +134,7 @@ const createHandlers = definitions => ({
text: (node, _parent, context) => {
const exit = context.enter('text');
// https://api.slack.com/reference/surfaces/formatting#escaping
const text = node.value
.replace(/&/g, '&amp;')
.replace(/<(?!@)/g, '&lt;')
.replace(/(?<!@[A-Z0-9]+)>/g, '&gt;');
const text = escapeSpecials(node.value);
exit();

// Do we need more escaping like the default handler uses?
Expand Down

0 comments on commit 05842b5

Please sign in to comment.