Skip to content

Commit

Permalink
feat: add @link supports
Browse files Browse the repository at this point in the history
[converter]
  • Loading branch information
MangelMaxime committed May 7, 2024
1 parent fd66863 commit 059e54c
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 3 deletions.
27 changes: 24 additions & 3 deletions src/Glutinum.Converter/Printer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,10 @@ let private codeInline (line: string) =
Regex("`(?<code>[^`]*)`")
.Replace(line, (fun m -> $"""<c>{m.Groups.["code"].Value}</c>"""))

let private codeBlock (line: string) =
let private codeBlock (text: string) =
Regex("```(?<lang>\S*)(?<code>[^`]+)```", RegexOptions.Multiline)
.Replace(
line,
text,
(fun m ->
let lang = m.Groups.["lang"].Value
let code = m.Groups.["code"].Value
Expand All @@ -295,7 +295,28 @@ let private codeBlock (line: string) =
)
)

let private transformToXmlDoc (line: string) = line |> codeBlock |> codeInline
let private link (text: string) =
// tsdoc link format is {@link link|customText}
// However when getting the text after `getDocumentationComment` and `displayPartsToString`
// the `|` seems to be replaced by a space
// Regex below tries to handle both cases
Regex("\{@link\s+(?<link>[^\s}|]+)\s*((\|)?\s*(?<customText>[^}]+))?\}")
.Replace(
text,
(fun m ->
let link = m.Groups.["link"].Value.Trim()
let customText = m.Groups.["customText"].Value

if String.IsNullOrWhiteSpace customText then
$"""<see href="{link}">{link}</see>"""
else
let customText = customText.Trim()
$"""<see href="{link}">{customText}</see>" """.TrimEnd()
)
)

let private transformToXmlDoc (line: string) =
line |> codeBlock |> codeInline |> link

let private printBlockTag
(printer: Printer)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Links can point to a URL:
*
* {@link my-control-library#Button }
* {@link my-control-library#Button | the Button class}
* {@link @microsoft/my-control-library/lib/Button#Button | the Button class}
*/
declare function isInlineTag(tagName: string): boolean;
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module rec Glutinum

open Fable.Core
open Fable.Core.JsInterop
open System

[<Erase>]
type Exports =
/// <summary>
/// Links can point to a URL:
///
/// <see href="my-control-library#Button">my-control-library#Button</see>
/// <see href="my-control-library#Button">the Button class</see>"
/// <see href="@microsoft/my-control-library/lib/Button#Button">the Button class</see>"
/// </summary>
[<Import("isInlineTag", "module")>]
static member isInlineTag (tagName: string) : bool = nativeOnly

(***)
#r "nuget: Fable.Core"
(***)
7 changes: 7 additions & 0 deletions tests/specs/references/documentation/link/urlOnly.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Links can point to a URL:
*
* {@link https://github.com/microsoft/tsdoc}
* {@link https://github.com/microsoft/tsdoc }
*/
declare function isInlineTag(tagName: string): boolean;
20 changes: 20 additions & 0 deletions tests/specs/references/documentation/link/urlOnly.fsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module rec Glutinum

open Fable.Core
open Fable.Core.JsInterop
open System

[<Erase>]
type Exports =
/// <summary>
/// Links can point to a URL:
///
/// <see href="https://github.com/microsoft/tsdoc">https://github.com/microsoft/tsdoc</see>
/// <see href="https://github.com/microsoft/tsdoc">https://github.com/microsoft/tsdoc</see>
/// </summary>
[<Import("isInlineTag", "module")>]
static member isInlineTag (tagName: string) : bool = nativeOnly

(***)
#r "nuget: Fable.Core"
(***)
7 changes: 7 additions & 0 deletions tests/specs/references/documentation/link/urlWithLabel.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Links can point to a URL:
*
* {@link https://github.com/microsoft/tsdoc | TSDoc}
* {@link https://github.com/microsoft/tsdoc | TSDoc }
*/
declare function isInlineTag(tagName: string): boolean;
20 changes: 20 additions & 0 deletions tests/specs/references/documentation/link/urlWithLabel.fsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module rec Glutinum

open Fable.Core
open Fable.Core.JsInterop
open System

[<Erase>]
type Exports =
/// <summary>
/// Links can point to a URL:
///
/// <see href="https://github.com/microsoft/tsdoc">TSDoc</see>"
/// <see href="https://github.com/microsoft/tsdoc">TSDoc</see>"
/// </summary>
[<Import("isInlineTag", "module")>]
static member isInlineTag (tagName: string) : bool = nativeOnly

(***)
#r "nuget: Fable.Core"
(***)

0 comments on commit 059e54c

Please sign in to comment.