Skip to content

Commit

Permalink
fix: Check for duplicate page titles not file names.
Browse files Browse the repository at this point in the history
  • Loading branch information
andymac4182 committed Apr 29, 2023
1 parent b5d6bb9 commit 540b1f9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
2 changes: 2 additions & 0 deletions packages/cli/Dockerfile
Expand Up @@ -4,4 +4,6 @@ COPY ./dist /app

ENV NODE_OPTIONS=--enable-source-maps

WORKDIR "/content"

CMD ["node", "/app/index.js"]
2 changes: 1 addition & 1 deletion packages/lib/src/Settings.ts
Expand Up @@ -94,7 +94,7 @@ export class EnvironmentVariableSettingsLoader extends SettingsLoader {

export class ConfigFileSettingsLoader extends SettingsLoader {
private configPath: string = path.join(
process.env.HOME ?? "",
process.cwd() ?? "",
".markdown-confluence.json"
);

Expand Down
33 changes: 22 additions & 11 deletions packages/lib/src/TreeLocal.ts
Expand Up @@ -32,18 +32,11 @@ const createTreeNode = (name: string): LocalAdfFileTreeNode => ({
const addFileToTree = (
treeNode: LocalAdfFileTreeNode,
file: MarkdownFile,
relativePath: string,
fileNames: Set<string>
relativePath: string
) => {
const [folderName, ...remainingPath] = relativePath.split(path.sep);

if (remainingPath.length === 0) {
if (fileNames.has(file.fileName)) {
throw new Error(
`File name "${file.fileName}" is not unique across all folders.`
);
}
fileNames.add(file.fileName);
const adfFile = mdToADFConverter.convertMDtoADF(file);
treeNode.children.push({
...createTreeNode(folderName),
Expand All @@ -59,7 +52,7 @@ const addFileToTree = (
treeNode.children.push(childNode);
}

addFileToTree(childNode, file, remainingPath.join(path.sep), fileNames);
addFileToTree(childNode, file, remainingPath.join(path.sep));
}
};

Expand Down Expand Up @@ -107,14 +100,32 @@ export const createFolderStructure = (
markdownFiles.map((file) => file.absoluteFilePath)
);
const rootNode = createTreeNode(commonPath);
const fileNames = new Set<string>();

markdownFiles.forEach((file) => {
const relativePath = path.relative(commonPath, file.absoluteFilePath);
addFileToTree(rootNode, file, relativePath, fileNames);
addFileToTree(rootNode, file, relativePath);
});

processNode(commonPath, rootNode);

checkUniquePageTitle(rootNode);

return rootNode;
};

function checkUniquePageTitle(
rootNode: LocalAdfFileTreeNode,
pageTitles: Set<string> = new Set<string>()
) {
const currentPageTitle = rootNode.file?.pageTitle ?? "";

if (pageTitles.has(currentPageTitle)) {
throw new Error(
`Page title "${currentPageTitle}" is not unique across all files.`
);
}
pageTitles.add(currentPageTitle);
rootNode.children.forEach((child) =>
checkUniquePageTitle(child, pageTitles)
);
}

0 comments on commit 540b1f9

Please sign in to comment.