Skip to content

Build your personal knowledge base with Trilium Notes

fix(export/markdown): proper language tag for Nginx

Elian Doran8c71e6aa9040
diff --git a/spec/support/utils.ts b/spec/support/utils.ts
index a616c0078f..8c796a3d3b 100644
--- a/spec/support/utils.ts
+++ b/spec/support/utils.ts
@@ -30,8 +30,11 @@
* @param strings
* @returns
*/
-export function trimIndentation(strings: TemplateStringsArray) {
- const str = strings.toString();
+export function trimIndentation(strings: TemplateStringsArray, ...values: any[]) {
+ // Combine the strings with the values using interpolation
+ let str = strings.reduce((acc, curr, index) => {
+ return acc + curr + (values[index] !== undefined ? values[index] : '');
+ }, '');
// Count the number of spaces on the first line.
let numSpaces = 0;
diff --git a/src/services/export/md.spec.ts b/src/services/export/md.spec.ts
index 405cf9db7b..49260cbfab 100644
--- a/src/services/export/md.spec.ts
+++ b/src/services/export/md.spec.ts
@@ -3,42 +3,34 @@ import markdownExportService from "./md.js";
import { trimIndentation } from "../../../spec/support/utils.js";
describe("Markdown export", () => {
- it("trims language tag for code blocks", () => {
- const html = trimIndentation`\
- <p>A diff:</p>
- <pre><code class="language-text-x-diff">Hello
- -world
- +worldy
- </code></pre>`;
- const expected = trimIndentation`\
- A diff:
-
- \`\`\`diff
- Hello
- -world
- +worldy
-
- \`\`\``;
-
- expect(markdownExportService.toMarkdown(html)).toBe(expected);
- });
-
- it("rewrites frontend script JavaScript code block", () => {
- const html = `<pre><code class="language-application-javascript-env-frontend">Hello</code></pre>`;
- const expected = trimIndentation`\
- \`\`\`javascript
- Hello
- \`\`\``;
- expect(markdownExportService.toMarkdown(html)).toBe(expected);
- });
- it("rewrites backend script JavaScript code block", () => {
- const html = `<pre><code class="language-application-javascript-env-backend">Hello</code></pre>`;
- const expected = trimIndentation`\
- \`\`\`javascript
- Hello
- \`\`\``;
- expect(markdownExportService.toMarkdown(html)).toBe(expected);
+ it("exports correct language tag for known languages", () => {
+ const conversionTable = {
+ "language-text-x-nginx-conf": "nginx",
+ "language-x-diff": "diff",
+ "language-application-javascript-env-frontend": "javascript",
+ "language-application-javascript-env-backend": "javascript"
+ };
+
+ for (const [ a, b ] of Object.entries(conversionTable)) {
+ const html = trimIndentation`\
+ <p>A diff:</p>
+ <pre><code class="${a}">Hello
+ -world
+ +worldy
+ </code></pre>`;
+ const expected = trimIndentation`\
+ A diff:
+
+ \`\`\`${b}
+ Hello
+ -world
+ +worldy
+
+ \`\`\``;
+
+ expect(markdownExportService.toMarkdown(html)).toBe(expected);
+ }
});
it("removes auto tag for code blocks", () => {
diff --git a/src/services/export/md.ts b/src/services/export/md.ts
index 7512655b08..cb4891b898 100644
--- a/src/services/export/md.ts
+++ b/src/services/export/md.ts
@@ -48,6 +48,8 @@ function rewriteLanguageTag(source: string) {
case "application-javascript-env-frontend":
case "application-javascript-env-backend":
return "javascript";
+ case "text-x-nginx-conf":
+ return "nginx";
default:
return source.split("-").at(-1);
}