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 2 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
14 changes: 12 additions & 2 deletions lib/ExpensiMark.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ 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> | {}

export default class ExpensiMark {
rules: Rule[];
htmlToMarkdownRules: Rule[];
Expand Down Expand Up @@ -57,13 +66,14 @@ export default class ExpensiMark {
*
* @param htmlString
*/
htmlToMarkdown(htmlString: string): string;
htmlToMarkdown(htmlString: string, personalDetails: PersonalDetails): string;
Copy link

@ArekChr ArekChr Nov 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be treated as metadata, allowing us to pass various data through the second parameter.

E.g.

const metadata = { personalDetails }
htmlToMarkdown(html, metadata)
const metadata = { reportDetails }
htmlToMarkdown(html, metadata)

same in htmlToText.
WDYT?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the second parameter be optional?

Suggested change
htmlToMarkdown(htmlString: string, personalDetails: PersonalDetails): string;
htmlToMarkdown(htmlString: string, personalDetails?: PersonalDetails): string;

/**
* Convert HTML to text
*
* @param htmlString
* @param personalDetails
*/
htmlToText(htmlString: string): string;
htmlToText(htmlString: string, personalDetails: PersonalDetails): string;
/**
* Modify text for Quotes replacing chevrons with html elements
*
Expand Down
36 changes: 33 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,10 @@ export default class ExpensiMark {
* Replaces HTML with markdown
*
* @param {String} htmlString
*
* @param {Object} personalDetails
* @returns {String}
*/
htmlToMarkdown(htmlString) {
htmlToMarkdown(htmlString, personalDetails = {}) {
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 +667,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 +680,17 @@ export default class ExpensiMark {
* Convert HTML to text
*
* @param {String} htmlString
* @param {Object} personalDetails
* @returns {String}
*/
htmlToText(htmlString) {
htmlToText(htmlString, personalDetails = {}) {
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