Skip to content

Commit ca031fa

Browse files
committed
translator: register section anchors for all editors
To help ensure anchor links to section work across all editor types and on all Confluence variants, ensure we always inject section anchors. Historically, this extension would only create anchors if required. Sections should have their own identifiers pre-made by Confluence. However, the identifiers are not consistent between different variations of Confluence and certain features (e.g. ac:links to anchors) expect certain anchor names. Over time we duplicated some anchors to help make things smooth when the new Confluence editor was made. Unfortunately, recent testing still showed not all links would function as expected. This commit tries to cover all anchor cases by always forcefully creating anchors in sections (and compatibility anchors), no matter what the editor is. When testing on Confluence Data Center, references appeared to work as they always have. With Confluence Cloud, references appear to function across different editors with when checking reference tests and inspecting `contents` usage. Signed-off-by: James Knight <git@jdknight.me>
1 parent 3ed6176 commit ca031fa

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

sphinxcontrib/confluencebuilder/storage/translator.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -273,21 +273,22 @@ def visit_title(self, node):
273273
else:
274274
docname = self.docname
275275

276-
# For v2, will will generate section anchors inside the title
277-
# area for the following reasons:
276+
# Generate section anchors inside the title area for the following
277+
# reasons:
278278
# - We want to create inside the header inside if we input anchors
279279
# before the header, it increase the space above the anchor
280280
# due to how v2 styles a page.
281281
# - We are generating compatible anchor links (prefixed with the
282282
# repsective document name) which helps allow `ac:link` macros
283283
# properly link when coming from v1 or v2 editor pages.
284-
if self.v2 and 'names' in node.parent:
284+
# - Helps support anchor links for legacy editor on Confluence Cloud
285+
if 'names' in node.parent:
285286
for name in node.parent['names']:
286287
anchor = name.replace(' ', '-')
287288
target_name = f'{docname}/#{anchor}'
288289
target = self.state.target(target_name)
289290
if target and target not in new_targets:
290-
self._build_anchor(node, target)
291+
self._build_anchor(node, target, force_compat=True)
291292
new_targets.append(target)
292293

293294
# For MyST sections with an auto-generated slug, we will use this
@@ -3497,7 +3498,7 @@ def _build_id_anchors(self, node):
34973498
for id_ in node['ids']:
34983499
self._build_anchor(node, id_)
34993500

3500-
def _build_anchor(self, node, anchor):
3501+
def _build_anchor(self, node, anchor, *, force_compat=False):
35013502
"""
35023503
build an anchor on a page
35033504
@@ -3514,14 +3515,15 @@ def _build_anchor(self, node, anchor):
35143515
Args:
35153516
node: the node adding the anchor
35163517
anchor: the name of the anchor to create
3518+
force_compat (optional): always force compat anchor
35173519
"""
35183520

35193521
self.verbose(f'build anchor ({self.docname}): {anchor}')
35203522
self.body.append(self.start_ac_macro(node, 'anchor'))
35213523
self.body.append(self.build_ac_param(node, '', anchor))
35223524
self.body.append(self.end_ac_macro(node, suffix=''))
35233525

3524-
if self.v2:
3526+
if self.builder.cloud or self.v2 or force_compat:
35253527
doctitle = self.state.title(self.docname)
35263528
doctitle = self.encode(doctitle.replace(' ', ''))
35273529

0 commit comments

Comments
 (0)