From 0e0e072cf064ef8e369f8ea718f727965eda35a7 Mon Sep 17 00:00:00 2001 From: Niyazbek Torekeldi <78027392+Tokesh@users.noreply.github.com> Date: Sat, 28 Dec 2024 04:52:03 +0500 Subject: [PATCH] Adjust Vale rules to ignore links (#753) * ignoring hyperlinks in descriptions for vale Signed-off-by: Tokesh * hotfix linter Signed-off-by: Tokesh * deleting not necessary sign to pass ci linter Signed-off-by: Tokesh * added separate method and tests Signed-off-by: Tokesh * added expect with contain Signed-off-by: Tokesh * added tests and fixing code style Signed-off-by: Tokesh * hotfix code style in keepdescriptions Signed-off-by: Tokesh * chore: added spaces outside of regex match Signed-off-by: Tokesh * chore: formatting code by linter Signed-off-by: Tokesh --------- Signed-off-by: Tokesh --- tools/src/prepare-for-vale/KeepDescriptions.ts | 16 ++++++++++++++-- tools/tests/prepare-for-vale/fixtures/spec.txt | 6 +++--- tools/tests/prepare-for-vale/fixtures/spec.yaml | 6 +++--- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/tools/src/prepare-for-vale/KeepDescriptions.ts b/tools/src/prepare-for-vale/KeepDescriptions.ts index e680da819..fa5699e58 100644 --- a/tools/src/prepare-for-vale/KeepDescriptions.ts +++ b/tools/src/prepare-for-vale/KeepDescriptions.ts @@ -41,11 +41,16 @@ export default class KeepDescriptions { if (line.match(/^[\s]+((description|x-deprecation-message): \|)/)) { inside_text = true } else if (line.match(/^[\s]+((description|x-deprecation-message):)[\s]+/)) { - fs.writeSync(writer, this.prune_vars(this.prune(line, /(description|x-deprecation-message):/, ' '))) + let cleaned_line = this.prune(line, /(description|x-deprecation-message):/, ' ') + cleaned_line = this.prune_vars(cleaned_line) + cleaned_line = this.remove_links(cleaned_line) + fs.writeSync(writer, cleaned_line) } else if (inside_text && line.match(/^[\s]*[\w\\$]*:/)) { inside_text = false } else if (inside_text) { - fs.writeSync(writer, this.prune_vars(line)) + let cleaned_line = this.remove_links(line) + cleaned_line = this.prune_vars(cleaned_line) + fs.writeSync(writer, cleaned_line) } if (line.length > 0) { fs.writeSync(writer, "\n") @@ -62,4 +67,11 @@ export default class KeepDescriptions { return Array(match.length + 1).join(char) }) } + + remove_links(line: string): string { + return line.replace(/\[([^\]]+)\]\([^)]+\)/g, (match, p1) => { + const spaces = ' '.repeat(match.length - p1.length - 1) + return ' ' + p1 + spaces + }) + } } diff --git a/tools/tests/prepare-for-vale/fixtures/spec.txt b/tools/tests/prepare-for-vale/fixtures/spec.txt index f9ada2c25..8070b934e 100644 --- a/tools/tests/prepare-for-vale/fixtures/spec.txt +++ b/tools/tests/prepare-for-vale/fixtures/spec.txt @@ -9,15 +9,15 @@ - For a successful response, this value is always true. On failure, an exception is returned instead. + For a successful response, this value is always true. On failure, an exception is returned instead Supported units . - The item level REST category class codes during indexing. + The item level REST category class codes during indexing link with a title . - Line one + Here is link one and link two . Line two diff --git a/tools/tests/prepare-for-vale/fixtures/spec.yaml b/tools/tests/prepare-for-vale/fixtures/spec.yaml index 6ca62c5de..f4688d63f 100644 --- a/tools/tests/prepare-for-vale/fixtures/spec.yaml +++ b/tools/tests/prepare-for-vale/fixtures/spec.yaml @@ -9,15 +9,15 @@ components: type: object properties: acknowledged: - description: For a successful response, this value is always true. On failure, an exception is returned instead. + description: For a successful response, this value is always true. On failure, an exception is returned instead [Supported units](https://opensearch.org/docs/latest/api-reference/units/). type: boolean ObjectWithMultilineDescriptionOneLine: description: |- - The item level REST category class codes during indexing. + The item level REST category class codes during indexing [link with a title](https://opensearch.org "title"). type: object ObjectWithMultilineDescriptionTwoLines: description: |- - Line one + Here is [link one](https://opensearch.org) and [link two](https://opensearch.org/). Line two ObjectWithAColonInDescription: type: object