From 209a6db5fc7a2551fe12159af034a321b28f14ad Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sun, 3 Sep 2023 20:48:29 +0300 Subject: [PATCH] File name fix --- src/main/kotlin/extractDocuments.kt | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/extractDocuments.kt b/src/main/kotlin/extractDocuments.kt index 6c2f1c1..bc1fbe3 100644 --- a/src/main/kotlin/extractDocuments.kt +++ b/src/main/kotlin/extractDocuments.kt @@ -56,6 +56,7 @@ internal suspend fun SpaceClient.extractFile( } private val imageRegex = """!\[(?.*)]\(/d/(?.*)\?f=0( "(?.*)")?\)""".toRegex() +private val fileNameRegex = "\\W+".toRegex() /** * Download single Space document @@ -90,13 +91,14 @@ internal suspend fun SpaceClient.downloadDocument( val id = it.groups["id"]?.value ?: error("Unexpected reference format: ${it.value}") val alt = it.groups["alt"]?.value?.ifBlank { null } val name = it.groups["name"]?.value?.ifBlank { null } - val imageName = name?.let { "$id-$name" } ?: alt?.let { "$id-$alt" } ?: id + val imageName = (name?.let { "$id-$name" } ?: alt?.let { "$id-$alt" } ?: id) + .replace(fileNameRegex, "_") val imageFile = imageDirectory.resolve(imageName) logger.info("Downloading image $id as $imageFile") launch(Dispatchers.IO) { extractAttachment(imageFile, id) } - "![${alt?:""}](images/$imageName)" + "![${alt ?: ""}](images/$imageName)" } markdownFilePath.writeText(newText, Charsets.UTF_8) } @@ -131,6 +133,7 @@ internal suspend fun SpaceClient.downloadDocumentFolder( documents.data.forEach { val document = projects.documents.getDocument(projectId, it.id) { body() + bodyType() title() id() } @@ -146,6 +149,7 @@ internal suspend fun SpaceClient.downloadDocumentFolder( ) ) { body() + bodyType() title() id() }