-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #192 from JetBrains/ktl-1138-generate-link
feat; KTL-1138: add method to generate a link to the playground
- Loading branch information
Showing
18 changed files
with
319 additions
and
154 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 was deleted.
Oops, something went wrong.
This file contains 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,51 @@ | ||
import { compressToBase64 } from 'lz-string'; | ||
|
||
import { isKeyOfObject } from '../utils/types'; | ||
import { TargetPlatforms, TargetPlatformsKeys } from '../utils/platforms'; | ||
|
||
import { | ||
escapeRegExp, | ||
MARK_PLACEHOLDER_CLOSE, | ||
MARK_PLACEHOLDER_OPEN, | ||
SAMPLE_END, | ||
SAMPLE_START, | ||
} from '../utils/escape'; | ||
|
||
type LinkOptions = { | ||
targetPlatform?: TargetPlatformsKeys | Lowercase<TargetPlatformsKeys>; | ||
compilerVersion?: string; | ||
}; | ||
|
||
/** | ||
* Assign the project to an employee. | ||
* @param {Object} code - The employee who is responsible for the project. | ||
* @param {Object} options - The employee who is responsible for the project. | ||
* @param {string} options.targetPlatform - The name of the employee. | ||
* @param {string} options.compilerVersion - The employee's department. | ||
*/ | ||
export function generateCrosslink(code: string, options?: LinkOptions) { | ||
const opts: { code: string } & LinkOptions = { | ||
code: code | ||
.replace(new RegExp(escapeRegExp(MARK_PLACEHOLDER_OPEN), 'g'), '') | ||
.replace(new RegExp(escapeRegExp(MARK_PLACEHOLDER_CLOSE), 'g'), '') | ||
.replace(new RegExp(escapeRegExp(SAMPLE_START), 'g'), '') | ||
.replace(new RegExp(escapeRegExp(SAMPLE_END), 'g'), ''), | ||
}; | ||
|
||
if (options && options.targetPlatform) { | ||
const target = | ||
options.targetPlatform && options.targetPlatform.toUpperCase(); | ||
|
||
if (!isKeyOfObject(target, TargetPlatforms)) | ||
throw new Error('Invalid target platform'); | ||
|
||
opts.targetPlatform = options.targetPlatform; | ||
} | ||
|
||
if (options && options.compilerVersion) | ||
opts.compilerVersion = options.compilerVersion; | ||
|
||
return `https://play.kotlinlang.org/editor/v1/${encodeURIComponent( | ||
compressToBase64(JSON.stringify(opts)), | ||
)}`; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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,33 @@ | ||
export const SAMPLE_START = '//sampleStart'; | ||
export const SAMPLE_END = '//sampleEnd'; | ||
|
||
export const MARK_PLACEHOLDER_OPEN = "[mark]"; | ||
export const MARK_PLACEHOLDER_CLOSE = "[/mark]"; | ||
|
||
|
||
/** | ||
* Use instead of @escape-string-regexp | ||
*/ | ||
|
||
export /*#__PURE__*/ function escapeRegExp(str) { | ||
return str.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1"); | ||
} | ||
|
||
/** | ||
* Unescape special characters from string | ||
* @param string | ||
* @returns {string} | ||
*/ | ||
export /*#__PURE__*/ function unEscapeString(string) { | ||
const tagsToReplace = { | ||
"<": "&lt;", | ||
">": "&gt;", | ||
"&": "&", | ||
" ": "%20" | ||
}; | ||
let unEscapedString = string; | ||
Object.keys(tagsToReplace).forEach(function (key) { | ||
unEscapedString = unEscapedString.replace(new RegExp(tagsToReplace[key], 'g'), key) | ||
}); | ||
return unEscapedString | ||
} |
Oops, something went wrong.