Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add mention user rule #600

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions lib/ExpensiMark.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ declare type Rule = {
pre?: (input: string) => string;
post?: (input: string) => string;
};

declare type PersonalDetail = {
accountID: string;
login?: string;
displayName?: string
}

declare type PersonalDetails = Record<string, PersonalDetail> | {}

declare type Metadata = {
personalDetails?: PersonalDetails
}

export default class ExpensiMark {
rules: Rule[];
htmlToMarkdownRules: Rule[];
Expand Down Expand Up @@ -56,14 +69,16 @@ export default class ExpensiMark {
* Replaces HTML with markdown
*
* @param htmlString
* @param metadata
*/
htmlToMarkdown(htmlString: string): string;
htmlToMarkdown(htmlString: string, metadata?: Metadata): string;
/**
* Convert HTML to text
*
* @param htmlString
* @param metadata
*/
htmlToText(htmlString: string): string;
htmlToText(htmlString: string, metadata?: Metadata): string;
/**
* Modify text for Quotes replacing chevrons with html elements
*
Expand Down
38 changes: 35 additions & 3 deletions lib/ExpensiMark.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ const MARKDOWN_LINK_REGEX = new RegExp(`\\[([^\\][]*(?:\\[[^\\][]*][^\\][]*)*)]\

const SLACK_SPAN_NEW_LINE_TAG = '<span class="c-mrkdwn__br" data-stringify-type="paragraph-break" style="box-sizing: inherit; display: block; height: unset;"></span>';

const userMentionReplacement = (openTag, accountID, closeTag, personalDetails = {}) => {
const user = _.get(personalDetails, accountID);
const displayNameOrLogin = _.get(user, 'login', '') || _.get(user, 'displayName', '');
return `${openTag}@${displayNameOrLogin}${closeTag}`;
};

export default class ExpensiMark {
constructor() {
/**
Expand Down Expand Up @@ -350,6 +356,16 @@ export default class ExpensiMark {
return `[${g4}](${email || g3})`;
},
},
{
name: 'mentionUser',
regex: /(<mention-user accountid="(.*?)">)(<\/mention-user>)/gi,
replacement: userMentionReplacement,
},
{
name: 'mentionUser',
regex: /(<mention-user accountID=(.*?)>)(<\/mention-user>)/gi,
replacement: userMentionReplacement,
},
];

/**
Expand Down Expand Up @@ -388,6 +404,11 @@ export default class ExpensiMark {
regex: /<style>.*?<\/style>/gi,
replacement: '',
},
{
name: 'mentionUser',
regex: /(<mention-user accountID=(.*?)>)(<\/mention-user>)/gi,
replacement: userMentionReplacement
},
{
name: 'stripTag',
regex: /(<([^>]+)>)/gi,
Expand Down Expand Up @@ -628,10 +649,11 @@ export default class ExpensiMark {
* Replaces HTML with markdown
*
* @param {String} htmlString
*
* @param {Object} metadata
* @returns {String}
*/
htmlToMarkdown(htmlString) {
htmlToMarkdown(htmlString, metadata = {}) {
const {personalDetails} = metadata;
let generatedMarkdown = htmlString;
const body = /<(body)(?:"[^"]*"|'[^']*'|[^'"><])*>(?:\n|\r\n)?([\s\S]*?)(?:\n|\r\n)?<\/\1>(?![^<]*(<\/pre>|<\/code>))/im;
const parseBodyTag = generatedMarkdown.match(body);
Expand All @@ -646,6 +668,10 @@ export default class ExpensiMark {
if (rule.pre) {
generatedMarkdown = rule.pre(generatedMarkdown);
}
if (rule.name === 'mentionUser') {
generatedMarkdown = generatedMarkdown.replace(rule.regex, (match, g1, g2, g3) => rule.replacement(g1, g2, g3, personalDetails));
return;
}
generatedMarkdown = generatedMarkdown.replace(rule.regex, rule.replacement);
});
return Str.htmlDecode(this.replaceBlockElementWithNewLine(generatedMarkdown));
Expand All @@ -655,12 +681,18 @@ export default class ExpensiMark {
* Convert HTML to text
*
* @param {String} htmlString
* @param {Object} metadata
* @returns {String}
*/
htmlToText(htmlString) {
htmlToText(htmlString, metadata = {}) {
const {personalDetails} = metadata;
let replacedText = htmlString;

this.htmlToTextRules.forEach((rule) => {
if (rule.name === 'mentionUser') {
replacedText = replacedText.replace(rule.regex, (match, g1, g2, g3) => rule.replacement(g1, g2, g3, personalDetails));
return;
}
replacedText = replacedText.replace(rule.regex, rule.replacement);
});

Expand Down
Loading