Skip to content

Commit

Permalink
Introduce an option to ignore broken links (PacoVK#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
lex committed Jul 26, 2024
1 parent 66f32a7 commit 0213320
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 16 deletions.
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,17 @@ The Captain will keep track of the pages it has created in Confluence. This is t

### Configuration

| Option | Description | Info |
| ---------------- | ---------------------------------------------------------------------------------------------------------------------------- | -------------------------- |
| confluence-api | URL to your Confluence API endpoint | **required** |
| confluence-space | The Confluence space key to publish the pages to | **required** |
| editor-version | The Confluence editor version to use to create pages | v1 (default) / v2 |
| filters | Specify paths or files that you want to publish to Confluence | [] (default) |
| ancestor-id | Specify the overall parent page for your docs. Needs to be the pageId of the parent page, not the DisplayName. | defaults to the space root |
| show-banner | Specify if all your pages should contain an info banner, that this pages were created by automation and changes may be lost. | false (default) |
| mapper | Specify a custom mapper to map the Antora pages to Confluence pages. | [] (default) |
| exclude-files | Specify files that should be excluded from the publishing process. Wildcards and Glob-patterns are supported | [] (default) |
| Option | Description | Info |
|---------------------|------------------------------------------------------------------------------------------------------------------------------|----------------------------|
| confluence-api | URL to your Confluence API endpoint | **required** |
| confluence-space | The Confluence space key to publish the pages to | **required** |
| editor-version | The Confluence editor version to use to create pages | v1 (default) / v2 |
| filters | Specify paths or files that you want to publish to Confluence | [] (default) |
| ancestor-id | Specify the overall parent page for your docs. Needs to be the pageId of the parent page, not the DisplayName. | defaults to the space root |
| ignore-broken-links | Specify whether to ignore broken link references to other pages that are not included | defaults to false |
| show-banner | Specify if all your pages should contain an info banner, that this pages were created by automation and changes may be lost. | false (default) |
| mapper | Specify a custom mapper to map the Antora pages to Confluence pages. | [] (default) |
| exclude-files | Specify files that should be excluded from the publishing process. Wildcards and Glob-patterns are supported | [] (default) |

#### Using Mappers

Expand Down
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const publishToConfluence = async (
baseUrl: new URL(destConfig.confluenceApi),
spaceKey: destConfig.confluenceSpace,
ancestorId: destConfig.ancestorId,
ignoreBrokenLinks: destConfig.ignoreBrokenLinks,
});
await confluenceClient.init();
const pageStructure = new Map();
Expand Down
3 changes: 3 additions & 0 deletions lib/client/ConfluenceClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ interface ConfluenceClientOptions {
spaceKey: string;
editorVersion: string;
ancestorId?: string;
ignoreBrokenLinks?: boolean;
}

const LOGGER = getLogger();
Expand All @@ -27,6 +28,7 @@ export abstract class ConfluenceClient {
readonly BASE_URL;
readonly AUTHORIZATION_HEADER;
readonly ANCESTOR_ID;
readonly IGNORE_BROKEN_LINKS;

constructor(config: ConfluenceClientOptions) {
this.BASE_URL = new URL(config.baseUrl.origin);
Expand All @@ -42,6 +44,7 @@ export abstract class ConfluenceClient {
this.API_V2_PATH = apiContext + this.API_V2_IDENTIFIER;
this.AUTHORIZATION_HEADER = this.buildAuthHeader();
this.ANCESTOR_ID = config.ancestorId;
this.IGNORE_BROKEN_LINKS = config.ignoreBrokenLinks ?? false;
}

async init() {
Expand Down
4 changes: 3 additions & 1 deletion lib/service/PageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ const publish = async (
outPutDir,
showBanner,
flatPages,
confluenceClient.IGNORE_BROKEN_LINKS,
);
if (confluencePage) {
const localHash = confluencePage.hash;
Expand Down Expand Up @@ -394,6 +395,7 @@ const processPage = (
outPutDir: string,
showBanner: boolean,
flatPages: any,
ignoreBrokenLinks: boolean,
) => {
LOGGER.info(`Processing ${page.fileName}`);
const baseUrl = path.join(process.cwd(), outPutDir, Path.dirname(page.fqfn));
Expand All @@ -411,7 +413,7 @@ const processPage = (
rewriteAdmonitionBlocks(content);
rewriteCodeBlocks(content);
rewriteMarks(content);
rewriteInternalLinks(content, page.fqfn, flatPages);
rewriteInternalLinks(content, page.fqfn, flatPages, ignoreBrokenLinks);
rewriteDescriptionLists(content);
rewriteCDATASections(content);

Expand Down
16 changes: 11 additions & 5 deletions lib/transformer/LinkTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ const rewriteInternalLinks = (
content: HTMLElement,
baseUrl: string,
flatPages: any,
ignoreBrokenLinks: boolean,
) => {
content.querySelectorAll("a[href]").forEach((a) => {
const href = a.getAttribute("href");
Expand All @@ -100,13 +101,18 @@ const rewriteInternalLinks = (
linkedPageFqfn = hrefWithAnchor[0];
anchor = hrefWithAnchor[1];
}
pageTitle = findLinkedPageInTree(
const linkedPage = findLinkedPageInTree(
flatPages,
path.join(path.dirname(baseUrl), linkedPageFqfn),
)?.pageTitle;
LOGGER.debug(
`Rewrite link to other page with title ${pageTitle} original link was ${href}`,
);
)
if (linkedPage) {
pageTitle = linkedPage.pageTitle;
LOGGER.debug(`Rewrite link to other page with title ${pageTitle}, original link was ${href}`);
} else {
if (!ignoreBrokenLinks) {
throw new Error(`The page '${baseUrl}' contains broken links, you can ignore this by setting 'ignore-broken-links' to true.`);
}
}
}
if (pageTitle && a.text) {
const linkMacro =
Expand Down
1 change: 1 addition & 0 deletions lib/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export type CaptainConfig = {
excludeFiles?: string[];
mapper?: PathMapper[];
filter?: PageFilter[];
ignoreBrokenLinks?: boolean;
};

export interface PathMapper {
Expand Down

0 comments on commit 0213320

Please sign in to comment.