Skip to content

Commit

Permalink
refactor(path): path formatter split into other function
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshinorin committed Dec 23, 2024
1 parent e0c8a18 commit 7530c1b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 10 deletions.
47 changes: 46 additions & 1 deletion __tests__/node/utils.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest';
import { removeTemplateEnginesSyntax } from '../../lib/utils';
import { removeTemplateEnginesSyntax, formatPath } from '../../lib/utils';

describe('removeTemplateEnginesSyntax', () => {
it('should remove {% raw %} and {% endraw %} from the text', () => {
Expand All @@ -25,4 +25,49 @@ describe('removeTemplateEnginesSyntax', () => {
const expectedOutput = "";
expect(removeTemplateEnginesSyntax(input)).toBe(expectedOutput);
});
});

describe('formatPath', () => {
it('should return the same path if it ends with a slash', () => {
const inputPath = "/example/path/";
const contentType = "article";
const expectedOutput = "/articles/example/path/";
expect(formatPath(inputPath, contentType)).toBe(expectedOutput);
});

it('should add a slash at the end if the path does not end with a slash', () => {
const inputPath = "/example/path";
const contentType = "article";
const expectedOutput = "/articles/example/path/";
expect(formatPath(inputPath, contentType)).toBe(expectedOutput);
});

it('should add a leading slash if the path does not start with a slash', () => {
const inputPath = "example/path/";
const contentType = "article";
const expectedOutput = "/articles/example/path/";
expect(formatPath(inputPath, contentType)).toBe(expectedOutput);
});

it('should add "/articles" to the path if contentType is "article" and path does not include "/articles"', () => {
const inputPath = "/example/path/";
const contentType = "article";
const expectedOutput = "/articles/example/path/";
expect(formatPath(inputPath, contentType)).toBe(expectedOutput);
});

it('should not add "/articles" to the path if contentType is not "article"', () => {
const inputPath = "/example/path/";
const contentType = "page";
const expectedOutput = "/example/path/";
expect(formatPath(inputPath, contentType)).toBe(expectedOutput);
});

// TODO: throw exception
it('should handle empty paths', () => {
const inputPath = "";
const contentType = "article";
const expectedOutput = "/articles/";
expect(formatPath(inputPath, contentType)).toBe(expectedOutput);
});
});
11 changes: 2 additions & 9 deletions lib/objectsGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const {
removeTemplateEnginesSyntax,
generateRobots,
} = require("../rust-lib/index.js");
const { formatPath } = require("../lib/utils.js");
const { externalLink } = require("./externalLink");
const micromatch = require("micromatch");
const { renderMarkdown } = require("../lib/markdown.js");
Expand Down Expand Up @@ -38,15 +39,7 @@ function objectsGenerator(content, contentType, url) {
}
}

let path = content.path.endsWith("/")
? content.path
: content.path.slice(0, content.path.lastIndexOf("/") + 1);
if (!path.startsWith("/")) {
path = "/" + path;
}
if (contentType === "article" && !path.includes("/articles")) {
path = "/articles" + path;
}
const path = formatPath(content.path, "article");
const c = removeTemplateEnginesSyntax(content._content);

if (
Expand Down
12 changes: 12 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,15 @@
export function removeTemplateEnginesSyntax(text) {
return text.replaceAll("{% raw %}", "").replaceAll("{% endraw %}", "");
}

export function formatPath(path, contentType) {
let p = path.endsWith("/") ? path : path + "/";

if (!p.startsWith("/")) {
p = "/" + p;
}
if (contentType === "article" && !p.includes("/articles")) {
p = "/articles" + p;
}
return p;
}

0 comments on commit 7530c1b

Please sign in to comment.