-
Notifications
You must be signed in to change notification settings - Fork 43
[FEAT] Expand code Link #116
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
eb6e209
feat(Helper): :sparkles: add helper message for code expand
iam-vipin 223ceb4
feat(Handle): :sparkles: add handle for links
iam-vipin cf1b0bb
feat(Premessage): :sparkles: Added PreMessageSentExtend
iam-vipin d08bf3a
refactor(Pr expand feature): :recycle: rename some function's
iam-vipin ebe8be1
refactor(Main-modal): :recycle: rewrite the function for more readabi…
iam-vipin 0516d02
feat(Pr expand feature): :sparkles: add enums
iam-vipin 2c26035
Merge branch 'main' into expand-code
iam-vipin dfe540b
refactor(expand-code): :recycle: refactor whole code to increase read…
iam-vipin 14494e3
refactor(expand-code): :art: improve formatting of code
iam-vipin af7e8df
Update EventHandler.ts
iam-vipin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| export enum GitHubURLEnum { | ||
| PREFIX = "blob/", | ||
| HOST = "github.com", | ||
| RAW_HOST = "raw.githubusercontent.com", | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import { IUser } from "@rocket.chat/apps-engine/definition/users"; | ||
| import { IHttp, IRead } from "@rocket.chat/apps-engine/definition/accessors"; | ||
| import { IMessage, IMessageAttachment } from "@rocket.chat/apps-engine/definition/messages"; | ||
| import { IRoom } from "@rocket.chat/apps-engine/definition/rooms"; | ||
| import { IMessageExtender } from "@rocket.chat/apps-engine/definition/accessors"; | ||
| import { TextObjectType } from "@rocket.chat/apps-engine/definition/uikit"; | ||
| import { GitHubURLEnum } from "../enum/GitHubURL"; | ||
|
|
||
| async function extractCodeSnippetFromURL(content: string, url: string): Promise<string> { | ||
| const lineRangeRegex: RegExp = /(?:L(\d+)+-L(\d+)|L(\d+))/; | ||
| const lineRangeMatch: RegExpMatchArray | null = url.match(lineRangeRegex); | ||
|
|
||
| if (lineRangeMatch) { | ||
| return extractCodeSnippetByLineRange(content, lineRangeMatch); | ||
| } | ||
|
|
||
| return ""; | ||
| } | ||
|
|
||
| function extractCodeSnippetByLineRange(content: string, lineRangeMatch: RegExpMatchArray): string { | ||
| const [_, startLine, endLine, singleLine] = lineRangeMatch; | ||
|
|
||
| const lineOffset = singleLine ? parseInt(singleLine) : parseInt(startLine) - 1; | ||
| const lineCount = singleLine ? 1 : parseInt(endLine) - parseInt(startLine) + 1; | ||
|
|
||
| const linesRegex = `(?:.*\n){${lineOffset}}(.*(?:\n.*){${lineCount}})`; | ||
| const lines = new RegExp(linesRegex); | ||
| const match = content.match(lines); | ||
|
|
||
| return match?.[1] ?? ""; | ||
| } | ||
|
|
||
| async function fetchGitHubContent(http: IHttp, modifiedUrl: string): Promise<string> { | ||
| const response: any = await http.get(modifiedUrl); | ||
| const { content } = response; | ||
| return content; | ||
| } | ||
|
|
||
| function buildCodeSnippetAttachment(codeSnippet: string, url: string): IMessageAttachment { | ||
| const attachment: IMessageAttachment = { | ||
| text: `\`\`\`\n${codeSnippet}\n\`\`\` \n[Show more...](${url})`, | ||
| type: TextObjectType.MARKDOWN, | ||
| }; | ||
| return attachment; | ||
| } | ||
|
|
||
| export async function handleGitHubCodeSegmentLink( | ||
| message: IMessage, | ||
| read: IRead, | ||
| http: IHttp, | ||
| user: IUser, | ||
| room: IRoom, | ||
| extend: IMessageExtender | ||
| ) { | ||
| const urlRegex: RegExp = /\bhttps?:\/\/github\.com\/\S+\b/; | ||
| const messageText: string = message.text!; | ||
| const urlMatch: RegExpMatchArray | null = messageText.match(urlRegex); | ||
| const url: string | undefined = urlMatch?.[0]; | ||
| let modifiedUrl: string = url?.replace(GitHubURLEnum.PREFIX, "")!; | ||
| modifiedUrl = modifiedUrl.replace(GitHubURLEnum.HOST, GitHubURLEnum.RAW_HOST); | ||
|
|
||
| const content: string = await fetchGitHubContent(http, modifiedUrl); | ||
| const codeSnippet = await extractCodeSnippetFromURL(content, modifiedUrl); | ||
|
|
||
| if (codeSnippet) { | ||
| const attachment: IMessageAttachment = buildCodeSnippetAttachment(codeSnippet, url!); | ||
| extend.addAttachment(attachment); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import { IMessage } from "@rocket.chat/apps-engine/definition/messages"; | ||
|
|
||
| export async function hasGitHubCodeSegmentLink(message: IMessage): Promise<Boolean> { | ||
| let lineNo: RegExp = | ||
| /https?:\/\/github\.com\/[A-Za-z0-9_-]+\/[A-Za-z0-9_.-]+\/blob\/[A-Za-z0-9_-]+\/.+/; | ||
|
|
||
| if (lineNo.test(message.text!)) { | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| export async function isGithubLink(message: IMessage) { | ||
| let githubLink: RegExp = /(?:https?:\/\/)?(?:www\.)?github\.com\//; | ||
| if (githubLink.test(message.text!)) { | ||
| return true; | ||
| } | ||
| return false; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.