Skip to content

Commit 4a5012b

Browse files
authored
Merge pull request #87 from Jolanrensen/export-as-html-resize
includeResizeScript
2 parents 97abf96 + be195b8 commit 4a5012b

5 files changed

Lines changed: 64 additions & 5 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ to provide documentation for other elements.
105105
To export a KDoc comment as HTML, you can use the `@ExportAsHtml` annotation.
106106
Create an annotation class named exactly "`ExportAsHtml`" and add the arguments `theme: Boolean` and
107107
`stripReferences: Boolean` (default both to `true`)
108-
(you can copy the code from [here](./kodex-common/src/main/kotlin/nl/jolanrensen/kodex/ExportAsHtml.kt)).
108+
(you can copy the code from [here](./kodex-common/src/main/kotlin/nl/jolanrensen/kodex/annotations/ExportAsHtml.kt)).
109109
Then, add the annotation to the element you want to export as HTML.
110110

111111
Inside the KDoc comment, you can mark a range of text to be exported as HTML by using the optional `@exportAsHtmlStart`

kodex-common/src/main/kotlin/nl/jolanrensen/kodex/annotations/ExportAsHtml.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import kotlin.annotation.AnnotationTarget.VALUE_PARAMETER
3030
* @param stripReferences Whether to strip `[references]` from the HTML file. Default is `true`.
3131
* This is useful when you want to include the HTML file in a website, where the references are not
3232
* needed or would break.
33+
* @param includeResizeScript Whether to include a script that helps height recalculation inside iFrames.
34+
* This is useful inside WriterSide. Default is `true`.
3335
*/
3436
@Target(
3537
CLASS,
@@ -46,4 +48,4 @@ import kotlin.annotation.AnnotationTarget.VALUE_PARAMETER
4648
TYPEALIAS,
4749
FILE,
4850
)
49-
annotation class ExportAsHtml(val theme: Boolean = true, val stripReferences: Boolean = true)
51+
annotation class ExportAsHtml(val theme: Boolean = true, val stripReferences: Boolean = true, val includeResizeScript: Boolean = true)

kodex-common/src/main/kotlin/nl/jolanrensen/kodex/html/HtmlRendering.kt

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import org.intellij.markdown.html.entities.EntityConverter
1616
import org.intellij.markdown.parser.LinkMap
1717
import org.intellij.markdown.parser.MarkdownParser
1818

19-
fun DocContent.renderToHtml(theme: Boolean, stripReferences: Boolean): String {
19+
fun DocContent.renderToHtml(theme: Boolean, stripReferences: Boolean, includeResizeScript: Boolean): String {
2020
// TODO https://github.com/JetBrains/markdown
2121
val flavour = GFMFlavourDescriptor(
2222
useSafeLinks = false,
@@ -103,6 +103,54 @@ fun DocContent.renderToHtml(theme: Boolean, stripReferences: Boolean): String {
103103
""".trimIndent(),
104104
)
105105
appendLine("</style>")
106+
if (includeResizeScript) {
107+
@Language("html")
108+
val b = appendLine(
109+
"""
110+
<script>
111+
function sendHeight() {
112+
const body = document.body;
113+
const html = document.documentElement;
114+
115+
const height = Math.max(
116+
body.scrollHeight,
117+
body.offsetHeight,
118+
html.clientHeight,
119+
html.scrollHeight,
120+
html.offsetHeight
121+
);
122+
123+
parent.postMessage({ type: 'iframeHeight', height }, '*');
124+
}
125+
126+
127+
function repeatHeightCalculation(maxRetries = 10, interval = 100) {
128+
let retries = 0;
129+
const intervalId = setInterval(() => {
130+
sendHeight();
131+
retries++;
132+
if (retries >= maxRetries) clearInterval(intervalId);
133+
}, interval);
134+
}
135+
136+
window.addEventListener('load', () => {
137+
repeatHeightCalculation();
138+
});
139+
140+
141+
const observer = new MutationObserver(() => repeatHeightCalculation(5, 50));
142+
observer.observe(document.body, {
143+
childList: true,
144+
subtree: true,
145+
characterData: true,
146+
attributes: true
147+
});
148+
149+
window.addEventListener('resize', sendHeight);
150+
</script>
151+
""".trimIndent()
152+
)
153+
}
106154
appendLine("</head>")
107155
}
108156
appendLine(body)

kodex-gradle-plugin/src/main/kotlin/nl/jolanrensen/kodex/RunKodexAction.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,9 +330,18 @@ abstract class RunKodexAction {
330330
?.second as? Boolean?
331331
?: true
332332

333+
val includeResizeScript = exportHtmlAnnotation.arguments
334+
.firstOrNull { (it, _) -> it == ExportAsHtml::includeResizeScript.name }
335+
?.second as? Boolean?
336+
?: true
337+
333338
val html = doc
334339
.getDocContentForHtmlRange()
335-
.renderToHtml(theme = addTheme, stripReferences = stripReferences)
340+
.renderToHtml(
341+
theme = addTheme,
342+
stripReferences = stripReferences,
343+
includeResizeScript = includeResizeScript,
344+
)
336345
val targetFile = File(htmlDir, doc.fullyQualifiedPath + ".html")
337346
try {
338347
targetFile.apply {

kodex-intellij-plugin/src/main/kotlin/nl/jolanrensen/kodex/syntaxHighlighting/ExportAsHtmlAnnotator.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ private fun exportToHtmlFile(annotationArguments: List<ValueArgument>, doc: Docu
128128

129129
val html = doc
130130
.getDocContentForHtmlRange()
131-
.renderToHtml(theme = theme, stripReferences = stripReferences)
131+
.renderToHtml(theme = theme, stripReferences = stripReferences, includeResizeScript = false)
132132
val file = File.createTempFile(doc.fullyQualifiedPath, ".html")
133133
file.writeText(html)
134134

0 commit comments

Comments
 (0)