import { UI5DeprecatedInfo , UI5SemanticModel , } from "@ui5-language-assistant/semantic-model-types" ; import { ReplaceFunction , forEach , unescape , concat , includes } from "lodash" ; export function getDeprecationPlainTextSnippet ( { title , deprecatedInfo , model , } ) { let text = deprecatedInfo . text ; if ( text !== undefined ) { text = convertJSDocToPlainText ( text , model ) ; if ( includes ( text , "\n" ) ) { text = text . substring ( 0 , text . indexOf ( "\n" ) ) + " ..." ; } } const doc = getDeprecationMessage ( { title , since : deprecatedInfo . since , text , } ) ; return doc ; } export function getDeprecationMessage ( { title , since , text , } ) { let contents = title ?? "Deprecated" ; contents += since ? ` since version ${ since }` : "" ; contents += "." ; contents += text ? ` ${ text }` : "" ; return contents ; } export function convertJSDocToPlainText ( jsdocDescription , model ) { return convertJSDoc ( jsdocDescription , "plaintext" , model ) ; } export function convertJSDocToMarkdown ( jsdocDescription , model ) { return convertJSDoc ( jsdocDescription , "markdown" , model ) ; } type ConvertTarget = "markdown" | "plaintext" ; const tagMatcherToReplacement = [ { matcher : /<i>(.+?)<\/i>/g , replacement : { markdown : "*$1*" , plaintext : "$1" } , } , { matcher : /<b>(.+?)<\/b>/g , replacement : { markdown : "**$1**" , plaintext : "$1" } , } , { matcher : /<strong>(.+?)<\/strong>/g , replacement : { markdown : "**$1**" , plaintext : "$1" } , } , { matcher : /<em>(.+?)<\/em>/g , replacement : { markdown : "***$1***" , plaintext : "$1" } , } , { matcher : /<h1>(.+?)<\/h1>/g , replacement : { markdown : "\n# $1\n\n" , plaintext : "\n$1\n" } , } , { matcher : /<h2>(.+?)<\/h2>/g , replacement : { markdown : "\n## $1\n\n" , plaintext : "\n$1\n" } , } , { matcher : /<h3>(.+?)<\/h3>/g , replacement : { markdown : "\n### $1\n\n" , plaintext : "\n$1\n" } , } , { matcher : /<h4>(.+?)<\/h4>/g , replacement : { markdown : "\n#### $1\n\n" , plaintext : "\n$1\n" } , } , { matcher : /<li>(.+?)<\/li>/g , replacement : { markdown : "\n* $1" , plaintext : "\n* $1" } , } , { matcher : /<ul>/g , replacement : { markdown : "" , plaintext : "" } } , { matcher : /<\/ul>/g , replacement : { markdown : "\n\n" , plaintext : "\n" } } , { matcher : /<code>(.+?)<\/code>/g , replacement : { markdown : "`$1`" , plaintext : "$1" } , } , { matcher : /<pre>([^]+?)<\/pre>/g , replacement : { markdown : "\n```javascript\n$1```\n" , plaintext : "\n$1\n" , } , } , { matcher : /<br\/>/g , replacement : { markdown : "\n" , plaintext : "\n" } } , { matcher : /<br>/g , replacement : { markdown : "\n" , plaintext : "\n" } } , { matcher : /<\/br>/g , replacement : { markdown : "\n" , plaintext : "\n" } } , { matcher : /.*/gm , replacement : { markdown : unescape , plaintext : unescape } , } , ] ; function convertJSDoc ( jsdoc , target , model ) { const allTagMatcherToReplacement = concat ( tagMatcherToReplacement , [ { matcher : /{@link (([^\s}]+)\s)?([^}]+)}/g , replacement : { markdown : ( all , _ , type , text ) : string => { return `[${ text }](${ getLink ( model , type ?? text ) })` ; } , plaintext : "$3" , } , } , ] ) ; let contents = jsdoc ; forEach ( allTagMatcherToReplacement , ( _ ) => { contents = replace ( contents , _ . matcher , _ . replacement [ target ] ) ; } ) ; return contents ; } function replace ( string , matcher , replacement ) { return string . replace ( matcher , replacement ) ; } export function getLink ( model , link ) { if ( link . startsWith ( "http:" ) || link . startsWith ( "https:" ) ) { return link ; } if ( model . version ) { return `https://sapui5.hana.ondemand.com/${ model . version }/#/api/${ link }` ; } return `https://sapui5.hana.ondemand.com/#/api/${ link }` ; }