import { Uri } from "monaco-editor" ; import * as monaco from "monaco-editor" ; /**
 * Common Uri identifiers
 */ enum Identifiers { cellUriMarker = "cell" , notebookUriMarker = "notebook" } /**
 * Map of language id to file extension.
 */ const languageToExtensionMap = new Map < string , string > ( ) ; /**
 * Document Monaco Uri helper to support conversions/detection for notebook and cells.
 */ export class DocumentUri { param uri static isDocument ( uri ) { const path = uri . path . replace ( /\.[^\/.]+$/ , "" ) ; return ! ! ( path && path . endsWith ( `/${ Identifiers . notebookUriMarker }` ) ) ; } param uri static isCell ( uri ) { const path = uri . path . replace ( /\.[^\/.]+$/ , "" ) ; return ! ! ( path && path . endsWith ( `/${ Identifiers . cellUriMarker }` ) ) ; } param uri static getDocumentIdFromDocumentUri ( uri ) { if ( DocumentUri . isDocument ( uri ) ) { const tokens = uri . path . split ( "/" ) ; if ( tokens . length === 3 ) { return tokens [ 1 ] ; } } return undefined ; } param uri static getDocumentIdFromCellUri ( uri ) { if ( DocumentUri . isCell ( uri ) ) { const tokens = uri . path . split ( "/" ) ; if ( tokens . length === 4 ) { return tokens [ 1 ] ; } } return undefined ; } param uri static getCellIdFromCellUri ( uri ) { if ( DocumentUri . isCell ( uri ) ) { const tokens = uri . path . split ( "/" ) ; if ( tokens . length === 4 ) { return tokens [ 2 ] ; } } return undefined ; } param id static createDocumentUri ( id , languageId ) { const fileExtension = DocumentUri . getFileExtension ( languageId ) ; return Uri . file ( `${ id }/${ Identifiers . notebookUriMarker }${ fileExtension }` ) ; } param documentId param cellId static createCellUri ( documentId , cellId , languageId ) { const fileExtension = DocumentUri . getFileExtension ( languageId ) ; return Uri . file ( `${ documentId }/${ cellId }/${ Identifiers . cellUriMarker }${ fileExtension }` ) ; } param languageId private static getFileExtension ( languageId ) { if ( ! languageToExtensionMap . has ( languageId ) ) { monaco . languages . getLanguages ( ) . forEach ( ( metadata ) => { languageToExtensionMap . set ( metadata . id , metadata . extensions && metadata . extensions . length > 0 ? metadata . extensions [ 0 ] : "" ) ; } ) ; } return languageToExtensionMap . get ( languageId ) || "" ; } }