Skip to content

Commit

Permalink
filter links in descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
jenschude committed Feb 7, 2024
1 parent 2f8846e commit bbf9418
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.vrap.codegen.languages.extensions

import io.vrap.rmf.codegen.doc.toHtml
import io.vrap.rmf.raml.model.types.AnnotationsFacet
import io.vrap.rmf.raml.model.types.DescriptionFacet
import io.vrap.rmf.raml.model.types.ObjectInstance
Expand Down Expand Up @@ -33,6 +32,13 @@ fun DescriptionFacet.toComment(empty: String = ""): String {
}
}

fun DescriptionFacet.toHtml() = this
.description
?.value
?.let(PARSER::parse)
?.let(HTML_RENDERER::render)
?.trim()

fun String.filterLinks(): String {
return Jsoup.clean(this, "", Safelist.basic().removeTags("a"), outputSettings)
.replace("", "€")
Expand All @@ -56,7 +62,7 @@ fun StringInstance.toComment(): String? {
if (enumValues?.value is ObjectInstance) {
val description = (enumValues.value as ObjectInstance).getValue(value)
return if (description is StringInstance) {
description.value?.let(PARSER::parse)?.let(HTML_RENDERER::render)?.let { "/**\n${it.lines().map { '\t' + it }.joinToString(separator = "\n")}\n*/" }
description.value?.let(PARSER::parse)?.let(HTML_RENDERER::render)?.let { "/**\n${it.lines().map { '\t' + it }.joinToString(separator = "\n")}\n*/" }?.filterLinks()
} else {
null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import io.vrap.rmf.codegen.CodeGeneratorConfig
import io.vrap.rmf.codegen.di.RamlApiProvider
import io.vrap.rmf.codegen.di.RamlGeneratorComponent
import io.vrap.rmf.codegen.di.RamlGeneratorModule
import io.vrap.rmf.codegen.io.MemoryDataSink
import io.vrap.rmf.codegen.types.VrapObjectType
import io.vrap.rmf.codegen.types.VrapType
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -142,4 +144,22 @@ class BuilderTestCodeGenerator {
Assertions.assertEquals(correctSimpleTypeClass, generatedSimleTypeClass)
Assertions.assertEquals(correctSimpleTypeInterface, generatedSimpleTypeInterface)
}

@Test
fun generateHtmlLinksToDocs() {
val generatorConfig = CodeGeneratorConfig(
basePackageName = "com/commercetools/importer",
outputFolder = Paths.get("build/gensrc"),
inlineExamples = true
)

val apiProvider = RamlApiProvider(Paths.get("src/test/resources/html-link.raml"))

val dataSink = MemoryDataSink()
val generatorModule = RamlGeneratorModule(apiProvider, generatorConfig, JavaBaseTypes, dataSink = dataSink)
val generatorComponent = RamlGeneratorComponent(generatorModule, JavaCompleteModule)
generatorComponent.generateFiles()

assertThat(dataSink.files).hasSize(8)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#%RAML 1.0
---
title: Commercetools Import API
baseUri: https://localhost

annotationTypes:
enumDescriptions:
description: |
Describes the values of an enum type.
type: object
properties:
//:
description: |
Adds a description for the enum value given by the key.
types:
Foo:
type: object
Bar:
type: object
properties:
foo:
type: Foo
description: |
some [Foo](ctp:api:type:Foo) property
FooBar:
type: string
enum:
- foo
- bar
(enumDescriptions):
foo: |
References an [Foo](ctp:api:type:Foo).
bar: |
References an [Bar](ctp:api:type:Bar).

0 comments on commit bbf9418

Please sign in to comment.