From: Stas Vilchik Date: Fri, 20 Jul 2018 07:32:10 +0000 (+0200) Subject: SONAR-11017 Fix generating of raw text of a doc page X-Git-Tag: 7.5~728 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=e391ded6cd9f35429d6f385fbaef3b7471dda3ab;p=sonarqube.git SONAR-11017 Fix generating of raw text of a doc page --- 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/strip-markdown.d.ts deleted file mode 100644 index 80d69fb86bf..00000000000 --- a/server/sonar-web/src/main/js/@types/strip-markdown.d.ts +++ /dev/null @@ -1,22 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2018 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * 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; -} diff --git a/server/sonar-web/src/main/js/@types/unist-util-visit.d.ts b/server/sonar-web/src/main/js/@types/unist-util-visit.d.ts new file mode 100644 index 00000000000..935ccf78a58 --- /dev/null +++ b/server/sonar-web/src/main/js/@types/unist-util-visit.d.ts @@ -0,0 +1,25 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * 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 '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: