Skip to content

Commit 132c6f6

Browse files
committed
feat(#65): Add rehype configuration namespace. Add missing test
1 parent 7819627 commit 132c6f6

File tree

13 files changed

+98
-15
lines changed

13 files changed

+98
-15
lines changed

.github/workflows/test-e2e.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ on:
77
push:
88
branches:
99
- main
10+
- origin/feat/65/rehype-codeblocks
1011

1112
permissions:
1213
contents: read

components/markdown-confluence-sync/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
* feat: Add code blocks transformation to Confluence code macro format.
1111
Code blocks are now converted to Confluence's structured code macro
1212
with syntax highlighting support. This feature is disabled by default
13-
and can be enabled via `codeBlocks` configuration option.
13+
and can be enabled via `rehype.codeBlocks` configuration option.
1414

1515
#### Changed
1616
#### Fixed

components/markdown-confluence-sync/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ The namespace for the configuration of this library is `markdown-confluence-sync
301301
| `confluence.noticeMessage` | `string` | Notice message to add at the beginning of the Confluence pages. | |
302302
| `confluence.noticeTemplate` | `string` | Template string to use for the notice message. | |
303303
| `confluence.dryRun` | `boolean` | Log create, update or delete requests to Confluence instead of really making them | `false` |
304-
| `codeBlocks` | `boolean` | Enable conversion of code blocks to Confluence code macro format with syntax highlighting. When disabled, code blocks remain as plain HTML pre/code tags. | `false` |
304+
| `rehype.codeBlocks` | `boolean` | Enable conversion of code blocks to Confluence code macro format with syntax highlighting. When disabled, code blocks remain as plain HTML pre/code tags. | `false` |
305305
| `dryRun` | `boolean` | Process markdown files without sending them to `confluence-sync`. Useful to early detection of possible errors in configuration, etc. Note that, requests that would be made to Confluence won't be logged, use `confluence.dryRun` for that, which also connects to Confluence to calculate the requests to do | `false` |
306306
| `config.readArguments` | `boolean` | Read configuration from arguments or not | `false` |
307307
| `config.readFile` | `boolean` | Read configuration from file or not | `false` |
@@ -497,13 +497,13 @@ Apart of supporting the most common markdown features, the library also supports
497497
* Code blocks - Markdown fenced code blocks can be converted to
498498
Confluence code macro format with syntax highlighting support. This
499499
feature is disabled by default but can be enabled via the
500-
`codeBlocks` configuration option.
500+
`rehype.codeBlocks` configuration option.
501501
* The plugin converts fenced code blocks to Confluence's
502502
`<ac:structured-macro ac:name="code">` format.
503503
* Language syntax highlighting is preserved when specified in the
504504
markdown code fence.
505505
* This feature is disabled by default for compatibility with older
506-
Confluence versions. Enable it by setting `codeBlocks: true`.
506+
Confluence versions. Enable it by setting `rehype.codeBlocks: true`.
507507
* For example, the following markdown code block:
508508
````markdown
509509
```javascript

components/markdown-confluence-sync/markdown-confluence-sync.config.cjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,7 @@ module.exports = {
4141
rootPageName: "Cross",
4242
},
4343
logLevel: "debug",
44+
rehype: {
45+
codeBlocks: true,
46+
},
4447
};

components/markdown-confluence-sync/src/lib/MarkdownConfluenceSync.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import type {
4141
const MODULE_NAME = "markdown-confluence-sync";
4242
const MARKDOWN_NAMESPACE = "markdown";
4343
const CONFLUENCE_NAMESPACE = "confluence";
44+
const REHYPE_CONFIG_NAMESPACE = "rehype";
4445

4546
const DEFAULT_CONFIG: Configuration["config"] = {
4647
readArguments: false,
@@ -146,6 +147,9 @@ export const MarkdownConfluenceSync: MarkdownConfluenceSyncConstructor = class M
146147

147148
const confluenceConfig =
148149
this._configuration.addNamespace(CONFLUENCE_NAMESPACE);
150+
const rehypeConfig = this._configuration.addNamespace(
151+
REHYPE_CONFIG_NAMESPACE,
152+
);
149153
const confluenceLogger = this._logger.namespace(CONFLUENCE_NAMESPACE);
150154

151155
this._markdownDocuments = new MarkdownDocuments({
@@ -160,6 +164,7 @@ export const MarkdownConfluenceSync: MarkdownConfluenceSyncConstructor = class M
160164
});
161165
this._confluenceSync = new ConfluenceSync({
162166
config: confluenceConfig,
167+
rehypeConfig: rehypeConfig,
163168
logger: confluenceLogger,
164169
mode: this._modeOption,
165170
});

components/markdown-confluence-sync/src/lib/confluence/ConfluenceSync.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ import type {
3434
NoticeTemplateOption,
3535
AuthenticationOptionDefinition,
3636
AuthenticationOption,
37+
RehypeCodeBlocksOptionDefinition,
38+
RehypeCodeBlocksOption,
3739
} from "./ConfluenceSync.types.js";
3840
import { ConfluencePageTransformer } from "./transformer/ConfluencePageTransformer.js";
3941
import type { ConfluencePageTransformerInterface } from "./transformer/ConfluencePageTransformer.types.js";
@@ -85,6 +87,12 @@ const authenticationOption: AuthenticationOptionDefinition = {
8587
type: "object",
8688
};
8789

90+
const rehypeCodeBlocksOption: RehypeCodeBlocksOptionDefinition = {
91+
name: "codeBlocks",
92+
type: "boolean",
93+
default: false,
94+
};
95+
8896
export const ConfluenceSync: ConfluenceSyncConstructor = class ConfluenceSync
8997
implements ConfluenceSyncInterface
9098
{
@@ -102,8 +110,9 @@ export const ConfluenceSync: ConfluenceSyncConstructor = class ConfluenceSync
102110
private _initialized = false;
103111
private _logger: LoggerInterface;
104112
private _modeOption: ModeOption;
113+
private _rehypeCodeBlocksOption: RehypeCodeBlocksOption;
105114

106-
constructor({ config, logger, mode }: ConfluenceSyncOptions) {
115+
constructor({ config, rehypeConfig, logger, mode }: ConfluenceSyncOptions) {
107116
this._urlOption = config.addOption(urlOption) as UrlOption;
108117
this._personalAccessTokenOption = config.addOption(
109118
personalAccessTokenOption,
@@ -125,6 +134,11 @@ export const ConfluenceSync: ConfluenceSyncConstructor = class ConfluenceSync
125134
authenticationOption,
126135
) as AuthenticationOption;
127136
this._dryRunOption = config.addOption(dryRunOption) as DryRunOption;
137+
138+
this._rehypeCodeBlocksOption = rehypeConfig.addOption(
139+
rehypeCodeBlocksOption,
140+
) as RehypeCodeBlocksOption;
141+
128142
this._modeOption = mode;
129143
this._logger = logger;
130144
}
@@ -189,6 +203,9 @@ export const ConfluenceSync: ConfluenceSyncConstructor = class ConfluenceSync
189203
rootPageName: this._rootPageNameOption.value,
190204
spaceKey: this._spaceKeyOption.value,
191205
logger: this._logger.namespace("transformer"),
206+
rehype: {
207+
codeBlocks: this._rehypeCodeBlocksOption.value,
208+
},
192209
});
193210

194211
this._confluenceSyncPages = new ConfluenceSyncPages({

components/markdown-confluence-sync/src/lib/confluence/ConfluenceSync.types.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ type NoticeMessageOptionValue = string;
2323
type NoticeTemplateOptionValue = string;
2424
type DryRunOptionValue = boolean;
2525

26+
type RehypeCodeBlocksOptionValue = boolean;
27+
2628
declare global {
2729
//eslint-disable-next-line @typescript-eslint/no-namespace
2830
namespace MarkdownConfluenceSync {
@@ -50,6 +52,10 @@ declare global {
5052
/** Confluence dry run */
5153
dryRun?: DryRunOptionValue;
5254
};
55+
rehype?: {
56+
/** Enable code blocks transformation to Confluence code macro */
57+
codeBlocks?: RehypeCodeBlocksOptionValue;
58+
};
5359
}
5460
}
5561
}
@@ -70,6 +76,8 @@ export type DryRunOptionDefinition = OptionDefinition<
7076
DryRunOptionValue,
7177
{ hasDefault: true }
7278
>;
79+
export type RehypeCodeBlocksOptionDefinition =
80+
OptionDefinition<RehypeCodeBlocksOptionValue>;
7381

7482
export type AuthenticationOptionDefinition =
7583
OptionDefinition<ConfluenceClientAuthenticationConfig>;
@@ -91,13 +99,20 @@ export type DryRunOption = OptionInterfaceOfType<
9199
{ hasDefault: true }
92100
>;
93101

102+
export type RehypeCodeBlocksOption = OptionInterfaceOfType<
103+
RehypeCodeBlocksOptionValue,
104+
{ hasDefault: true }
105+
>;
106+
94107
export interface ConfluenceSyncOptions {
95108
/** Configuration interface */
96109
config: ConfigNamespaceInterface;
97110
/** Logger interface */
98111
logger: LoggerInterface;
99112
/** Sync mode option */
100113
mode: ModeOption;
114+
/** Rehype configuration namespace */
115+
rehypeConfig: ConfigNamespaceInterface;
101116
}
102117

103118
/** Creates a ConfluenceSyncInterface interface */

components/markdown-confluence-sync/src/lib/confluence/transformer/ConfluencePageTransformer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export const ConfluencePageTransformer: ConfluencePageTransformerConstructor = c
5959
rootPageName,
6060
spaceKey,
6161
logger,
62-
codeBlocks,
62+
rehype: { codeBlocks },
6363
}: ConfluencePageTransformerOptions) {
6464
this._noticeMessage = noticeMessage;
6565
this._noticeTemplateRaw = noticeTemplate;

components/markdown-confluence-sync/src/lib/confluence/transformer/ConfluencePageTransformer.types.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@ import type { ConfluenceInputPage } from "@telefonica/confluence-sync";
66

77
import type { ConfluenceSyncPage } from "../ConfluenceSync.types.js";
88

9+
/**
10+
* Options for Rehype plugins used in ConfluencePageTransformer
11+
*/
12+
export interface ConfluencePageTransformerRehypeOptions {
13+
/**
14+
* Enable code blocks transformation to Confluence code macro.
15+
* When enabled, markdown code blocks will be converted to Confluence's
16+
* structured code macro format with syntax highlighting support.
17+
* @default false
18+
*/
19+
codeBlocks?: boolean;
20+
}
21+
922
export interface ConfluencePageTransformerOptions {
1023
/** Confluence page notice message */
1124
noticeMessage?: string;
@@ -24,13 +37,8 @@ export interface ConfluencePageTransformerOptions {
2437
spaceKey: string;
2538
/** Logger */
2639
logger?: LoggerInterface;
27-
/**
28-
* Enable code blocks transformation to Confluence code macro.
29-
* When enabled, markdown code blocks will be converted to Confluence's
30-
* structured code macro format with syntax highlighting support.
31-
* @default false
32-
*/
33-
codeBlocks?: boolean;
40+
/** Rehype options */
41+
rehype: ConfluencePageTransformerRehypeOptions;
3442
}
3543

3644
/** Creates a ConfluencePageTransformer interface */

components/markdown-confluence-sync/src/lib/confluence/transformer/support/rehype/rehype-replace-code-blocks.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ function extractLanguage(codeElement: HastElement): string | undefined {
104104
return undefined;
105105
}
106106

107-
// className can be a string or an array of strings
107+
// className is always an array of strings, but we check it for safety
108+
// istanbul ignore next
108109
const classNames = Array.isArray(className) ? className : [className];
109110

110111
// Look for a class that starts with "language-"

0 commit comments

Comments
 (0)