]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-11017 Fix generating of raw text of a doc page
authorStas Vilchik <stas.vilchik@sonarsource.com>
Fri, 20 Jul 2018 07:32:10 +0000 (09:32 +0200)
committerSonarTech <sonartech@sonarsource.com>
Wed, 25 Jul 2018 18:21:21 +0000 (20:21 +0200)
server/sonar-web/package.json
server/sonar-web/src/main/js/@types/strip-markdown.d.ts [deleted file]
server/sonar-web/src/main/js/@types/unist-util-visit.d.ts [new file with mode: 0644]
server/sonar-web/src/main/js/apps/documentation/pages.ts
server/sonar-web/yarn.lock

index 43106601f5996154299a0ad753b130e84a8f529f..5fdb34c101754c2dc748f4f5d91efd2bdb292222 100644 (file)
@@ -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": {
     "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 (file)
index 80d69fb..0000000
+++ /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 (file)
index 0000000..935ccf7
--- /dev/null
@@ -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;
+}
index a81c2866aad56d2f1715d697c580cc88e45a0135..246ea1c3b42107ac2c00759f4d446ef50d39d788 100644 (file)
@@ -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, ' ');
+}
index 27b31b63ff4d1aaa50cf817f7ca717b659925b67..5046b41e5cbf5463414ddf56acb47c52f3e9e96f 100644 (file)
@@ -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: