-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(edit-content): add docs #29875
- Loading branch information
Showing
4 changed files
with
180 additions
and
33 deletions.
There are no files selected for viewing
29 changes: 0 additions & 29 deletions
29
core-web/libs/edit-content/src/lib/fields/dot-edit-content-binary-field/utils/editor.ts
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
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
53 changes: 53 additions & 0 deletions
53
core-web/libs/edit-content/src/lib/fields/dot-edit-content-file-field/utils/editor.ts
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,53 @@ | ||
/** | ||
* Extracts the file extension from a given file name. | ||
* | ||
* @param fileName - The name of the file from which to extract the extension. | ||
* @returns The file extension if present, otherwise an empty string. | ||
*/ | ||
export function extractFileExtension(fileName: string): string { | ||
const includesDot = fileName.includes('.'); | ||
|
||
if (!includesDot) { | ||
return ''; | ||
} | ||
|
||
return fileName.split('.').pop() || ''; | ||
} | ||
|
||
/** | ||
* Retrieves language information based on the provided file extension. | ||
* | ||
* @param extension - The file extension to get the language information for. | ||
* @returns An object containing the language id, MIME type, and extension. | ||
* | ||
* @example | ||
* ```typescript | ||
* const info = getInfoByLang('vtl'); | ||
* console.log(info); | ||
* // Output: { lang: 'html', mimeType: 'text/x-velocity', extension: '.vtl' } | ||
* ``` | ||
* | ||
* @remarks | ||
* If the extension is 'vtl', it returns a predefined set of values. | ||
* Otherwise, it searches through the Monaco Editor languages to find a match. | ||
* If no match is found, it defaults to 'text' for the language id, 'text/plain' for the MIME type, and '.txt' for the extension. | ||
*/ | ||
export function getInfoByLang(extension: string) { | ||
if (extension === 'vtl') { | ||
return { | ||
lang: 'html', | ||
mimeType: 'text/x-velocity', | ||
extension: '.vtl' | ||
}; | ||
} | ||
|
||
const language = monaco.languages | ||
.getLanguages() | ||
.find((language) => language.extensions?.includes(`.${extension}`)); | ||
|
||
return { | ||
lang: language?.id || 'text', | ||
mimeType: language?.mimetypes?.[0] || 'text/plain', | ||
extension: language?.extensions?.[0] || '.txt' | ||
}; | ||
} |