From e391ded6cd9f35429d6f385fbaef3b7471dda3ab Mon Sep 17 00:00:00 2001 From: Stas Vilchik Date: Fri, 20 Jul 2018 09:32:10 +0200 Subject: [PATCH] SONAR-11017 Fix generating of raw text of a doc page --- server/sonar-web/package.json | 2 +- ...ip-markdown.d.ts => unist-util-visit.d.ts} | 7 +++++-- .../src/main/js/apps/documentation/pages.ts | 20 ++++++++++++------- server/sonar-web/yarn.lock | 6 +----- 4 files changed, 20 insertions(+), 15 deletions(-) rename server/sonar-web/src/main/js/@types/{strip-markdown.d.ts => unist-util-visit.d.ts} (84%) diff --git a/server/sonar-web/package.json b/server/sonar-web/package.json index 43106601f59..5fdb34c1017 100644 --- a/server/sonar-web/package.json +++ b/server/sonar-web/package.json @@ -40,6 +40,7 @@ "redux-thunk": "2.2.0", "remark-custom-blocks": "2.2.0", "remark-toc": "5.0.0", + "unist-util-visit": "1.3.1", "whatwg-fetch": "2.0.4" }, "devDependencies": { @@ -113,7 +114,6 @@ "react-test-renderer": "16.2.0", "remark": "9.0.0", "remark-react": "4.0.3", - "strip-markdown": "3.0.1", "style-loader": "0.21.0", "ts-jest": "22.4.6", "ts-loader": "4.3.0", diff --git a/server/sonar-web/src/main/js/@types/strip-markdown.d.ts b/server/sonar-web/src/main/js/@types/unist-util-visit.d.ts similarity index 84% rename from server/sonar-web/src/main/js/@types/strip-markdown.d.ts rename to server/sonar-web/src/main/js/@types/unist-util-visit.d.ts index 80d69fb86bf..935ccf78a58 100644 --- a/server/sonar-web/src/main/js/@types/strip-markdown.d.ts +++ b/server/sonar-web/src/main/js/@types/unist-util-visit.d.ts @@ -17,6 +17,9 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -declare module 'strip-markdown' { - export default function stripMarkdown(): any; +declare module 'unist-util-visit' { + export default function visit( + ast: any, + visitor: (node: { type: string; value: string }) => void + ): void; } diff --git a/server/sonar-web/src/main/js/apps/documentation/pages.ts b/server/sonar-web/src/main/js/apps/documentation/pages.ts index a81c2866aad..246ea1c3b42 100644 --- a/server/sonar-web/src/main/js/apps/documentation/pages.ts +++ b/server/sonar-web/src/main/js/apps/documentation/pages.ts @@ -18,7 +18,7 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ import remark from 'remark'; -import strip from 'strip-markdown'; +import visit from 'unist-util-visit'; import { DocumentationEntry, DocumentationEntryScope } from './utils'; import * as Docs from './documentation.directory-loader'; import { separateFrontMatter, filterContent } from '../../helpers/markdown'; @@ -28,12 +28,7 @@ export default function getPages(): DocumentationEntry[] { return Docs.map((file: any) => { const parsed = separateFrontMatter(file.content); const content = filterContent(parsed.content); - - const text = remark() - .use(strip) - .processSync(content) - .contents.replace(/\n+/, ' ') - .trim(); + const text = getText(content); return { relativeName: file.path, @@ -50,3 +45,14 @@ export default function getPages(): DocumentationEntry[] { page.scope !== 'static' && (isSonarCloud() || page.scope !== 'sonarcloud') ); } + +function getText(content: string) { + const ast = remark().parse(content); + const texts: string[] = []; + visit(ast, node => { + if (node.type === `text` || node.type === `inlineCode`) { + texts.push(node.value); + } + }); + return texts.join(' ').replace(/\s+/g, ' '); +} diff --git a/server/sonar-web/yarn.lock b/server/sonar-web/yarn.lock index 27b31b63ff4..5046b41e5cb 100644 --- a/server/sonar-web/yarn.lock +++ b/server/sonar-web/yarn.lock @@ -8211,10 +8211,6 @@ strip-json-comments@~2.0.1: version "2.0.1" resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-2.0.1.tgz#3c531942e908c2697c0ec344858c286c7ca0a60a" -strip-markdown@3.0.1: - version "3.0.1" - resolved "https://registry.yarnpkg.com/strip-markdown/-/strip-markdown-3.0.1.tgz#bf4f1c04d03720ae76a01a111cef941d5255cf88" - style-loader@0.21.0: version "0.21.0" resolved "https://registry.yarnpkg.com/style-loader/-/style-loader-0.21.0.tgz#68c52e5eb2afc9ca92b6274be277ee59aea3a852" @@ -8647,7 +8643,7 @@ unist-util-stringify-position@^1.0.0, unist-util-stringify-position@^1.1.1: version "1.1.1" resolved "https://registry.yarnpkg.com/unist-util-stringify-position/-/unist-util-stringify-position-1.1.1.tgz#3ccbdc53679eed6ecf3777dd7f5e3229c1b6aa3c" -unist-util-visit@^1.0.0, unist-util-visit@^1.1.0, unist-util-visit@^1.3.0: +unist-util-visit@1.3.1, unist-util-visit@^1.0.0, unist-util-visit@^1.1.0, unist-util-visit@^1.3.0: version "1.3.1" resolved "https://registry.yarnpkg.com/unist-util-visit/-/unist-util-visit-1.3.1.tgz#c019ac9337a62486be58531bc27e7499ae7d55c7" dependencies: -- 2.39.5