--- /dev/null
+# sonar-docs
+
+## Formatting
+
+## Conditional Content
+
+With special comments you can mark a page or a part of the content to be displayed only on SonarCloud, SonarQube or the static documentation website.
+
+To display a page only in a certain context use the frontmatter option:
+
+```md
+---
+scope: sonarcloud (or sonarqube, or static)
+---
+```
+
+To display/hide a part of the content use special comments:
+
+```md
+<!-- sonarcloud -->
+this content is displayed only on SonarCloud
+<!-- /sonarcloud -->
+
+<!-- sonarqube -->
+this content is displayed in SonarQube and in the static website
+<!-- /sonarqube -->
+
+<!-- static -->
+this content is displayed only in the static website
+<!-- /static -->
+```
+
+You can also use inline comments:
+
+```md
+this content is displayed on <!-- sonarcloud -->SonarCloud<!-- /sonarcloud --><!-- sonarqube -->SonarQube<!-- /sonarqube -->
+```
let htmlWithInclusions = cutSonarCloudContent(page.html).replace(
/\<p\>@include (.*)\<\/p\>/,
(_, path) => {
- const chunk = data.allMarkdownRemark.edges.find(edge => edge.node.fields.slug === path);
+ const chunk = data.allMarkdownRemark.edges.find(
+ edge => edge.node.fields && edge.node.fields.slug === path
+ );
return chunk ? chunk.node.html : '';
}
);
*/
import remark from 'remark';
import strip from 'strip-markdown';
-import { DocumentationEntry } from './utils';
+import { DocumentationEntry, DocumentationEntryScope } from './utils';
import * as Docs from './documentation.directory-loader';
import { separateFrontMatter, filterContent } from '../../helpers/markdown';
import { isSonarCloud } from '../../helpers/system';
relativeName: file.path,
title: parsed.frontmatter.title,
order: Number(parsed.frontmatter.order || -1),
- scope:
- parsed.frontmatter.scope && parsed.frontmatter.scope.toLowerCase() === 'sonarcloud'
- ? ('sonarcloud' as 'sonarcloud')
- : undefined,
+ scope: parsed.frontmatter.scope
+ ? (parsed.frontmatter.scope.toLowerCase() as DocumentationEntryScope)
+ : undefined,
text,
content: file.content
};
- }).filter((page: DocumentationEntry) => isSonarCloud() || page.scope !== 'sonarcloud');
+ }).filter(
+ (page: DocumentationEntry) =>
+ page.scope !== 'static' && (isSonarCloud() || page.scope !== 'sonarcloud')
+ );
}
*/
import { sortBy } from 'lodash';
+export type DocumentationEntryScope = 'sonarqube' | 'sonarcloud' | 'static';
+
export interface DocumentationEntry {
content: string;
order: number;
relativeName: string;
- scope?: 'sonarcloud';
+ scope?: DocumentationEntryScope;
text: string;
title: string;
}
).toMatchSnapshot();
});
-it('should cut sonarqube/sonarcloud content', () => {
+it('should cut sonarqube/sonarcloud/static content', () => {
const content = `
some
sonarcloud
<!-- /sonarcloud -->
+<!-- static -->
+static
+<!-- /static -->
+
<!-- sonarqube -->
long
// Jest Snapshot v1, https://goo.gl/fbAQLP
-exports[`should cut sonarqube/sonarcloud content 1`] = `
+exports[`should cut sonarqube/sonarcloud/static content 1`] = `
<div
className="markdown"
>
</div>
`;
-exports[`should cut sonarqube/sonarcloud content 2`] = `
+exports[`should cut sonarqube/sonarcloud/static content 2`] = `
<div
className="markdown"
>
return data;
}
+/**
+ * @param {string} content
+ * @returns {string}
+ */
function filterContent(content) {
const { isSonarCloud } = require('../helpers/system');
- const beginning = isSonarCloud() ? '<!-- sonarqube -->' : '<!-- sonarcloud -->';
- const ending = isSonarCloud() ? '<!-- /sonarqube -->' : '<!-- /sonarcloud -->';
+ const contentWithoutStatic = cutConditionalContent(content, 'static');
+ return isSonarCloud()
+ ? cutConditionalContent(contentWithoutStatic, 'sonarqube')
+ : cutConditionalContent(contentWithoutStatic, 'sonarcloud');
+}
+
+/**
+ * @param {string} content
+ * @param {string} tag
+ * @returns {string}
+ */
+function cutConditionalContent(content, tag) {
+ const beginning = `<!-- ${tag} -->`;
+ const ending = `<!-- /${tag} -->`;
let newContent = content;
let start = newContent.indexOf(beginning);