Skip to content

Commit

Permalink
Emoji aren't rendered in code review (#6536)
Browse files Browse the repository at this point in the history
Fixes #5776
  • Loading branch information
alexr00 authored Dec 13, 2024
1 parent fb9890a commit f52c69d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
4 changes: 3 additions & 1 deletion build/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ module.exports.unicodeFilter = [
'!**/ThirdPartyNotices.txt',
'!**/LICENSE.{txt,rtf}',
'!**/LICENSE',
'!*.yml'
'!*.yml',
'!resources/emojis.json'
];

module.exports.indentationFilter = [
Expand All @@ -41,6 +42,7 @@ module.exports.indentationFilter = [
'!**/LICENSE.{txt,rtf}',
'!**/LICENSE',
'!**/*.yml',
'!resources/emojis.json',

// except multiple specific files
'!**/package.json',
Expand Down
1 change: 1 addition & 0 deletions resources/emojis.json

Large diffs are not rendered by default.

39 changes: 39 additions & 0 deletions src/common/emoji.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

// Copied from https://github.com/microsoft/vscode/blob/af33df91a45498435bc47f16444d91db4582ce48/extensions/git/src/emoji.ts

'use strict';
import { TextDecoder } from 'util';
import { ExtensionContext, Uri, workspace } from 'vscode';

const emojiRegex = /:([-+_a-z0-9]+):/g;

let emojiMap: Record<string, string> | undefined;
let emojiMapPromise: Promise<void> | undefined;

export async function ensureEmojis(context: ExtensionContext) {
if (emojiMap === undefined) {
if (emojiMapPromise === undefined) {
emojiMapPromise = loadEmojiMap(context);
}
await emojiMapPromise;
}
}

async function loadEmojiMap(context: ExtensionContext) {
const uri = (Uri as any).joinPath(context.extensionUri, 'resources', 'emojis.json');
emojiMap = JSON.parse(new TextDecoder('utf8').decode(await workspace.fs.readFile(uri)));
}

export function emojify(message: string) {
if (emojiMap === undefined) {
return message;
}

return message.replace(emojiRegex, (s, code) => {
return emojiMap?.[code] || s;
});
}
7 changes: 5 additions & 2 deletions src/github/prComment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import * as path from 'path';
import * as vscode from 'vscode';
import { IComment } from '../common/comment';
import { emojify, ensureEmojis } from '../common/emoji';
import Logger from '../common/logger';
import { DataUri } from '../common/uri';
import { ALLOWED_USERS, JSDOC_NON_USERS, PHPDOC_NON_USERS } from '../common/user';
Expand Down Expand Up @@ -219,7 +220,7 @@ export class GHPRComment extends CommentBase {
private _rawBody: string | vscode.MarkdownString;
private replacedBody: string;

constructor(context: vscode.ExtensionContext, comment: IComment, parent: GHPRCommentThread, private readonly githubRepositories?: GitHubRepository[]) {
constructor(private readonly context: vscode.ExtensionContext, comment: IComment, parent: GHPRCommentThread, private readonly githubRepositories?: GitHubRepository[]) {
super(parent);
this.rawComment = comment;
this.originalAuthor = {
Expand Down Expand Up @@ -407,6 +408,7 @@ ${lineContents}
}

private async replaceBody(body: string | vscode.MarkdownString): Promise<string> {
const emojiPromise = ensureEmojis(this.context);
Logger.trace('Replace comment body', GHPRComment.ID);
if (body instanceof vscode.MarkdownString) {
const permalinkReplaced = await this.replacePermalink(body.value);
Expand Down Expand Up @@ -435,7 +437,8 @@ ${lineContents}
});

const permalinkReplaced = await this.replacePermalink(linkified);
return this.postpendSpecialAuthorComment(this.replaceImg(this.replaceSuggestion(permalinkReplaced)));
await emojiPromise;
return this.postpendSpecialAuthorComment(emojify(this.replaceImg(this.replaceSuggestion(permalinkReplaced))));
}

protected async doSetBody(body: string | vscode.MarkdownString, refresh: boolean) {
Expand Down

0 comments on commit f52c69d

Please sign in to comment.