Skip to content

Commit a6d74ca

Browse files
sync-pr-commit-title, pr-first-commit-title - Don't omit backticks (#9012)
1 parent 756a0e2 commit a6d74ca

File tree

3 files changed

+52
-24
lines changed

3 files changed

+52
-24
lines changed

source/features/pr-first-commit-title.tsx

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,17 @@ import {insertTextIntoField, setFieldText} from 'text-field-edit';
66
import features from '../feature-manager.js';
77
import looseParseInt from '../helpers/loose-parse-int.js';
88
import observe from '../helpers/selector-observer.js';
9-
10-
function interpretNode(node: ChildNode): string | void {
11-
switch (node instanceof Element && node.tagName) {
12-
case false:
13-
case 'A': {
14-
return node.textContent;
15-
}
16-
17-
case 'CODE': {
18-
// Restore backticks that GitHub loses when rendering them
19-
return '`' + node.textContent + '`';
20-
}
21-
22-
default:
23-
// Ignore other nodes, like `<span>...</span>` that appears when commits have a body
24-
}
25-
}
9+
import parseRenderedText from '../github-helpers/parse-rendered-text.js';
2610

2711
function getFirstCommit(firstCommit: HTMLElement): {title: string; body: string | undefined} {
2812
const body = $optional('.Details-content--hidden pre', firstCommit)
2913
?.textContent
3014
.trim() ?? undefined;
3115

32-
const title = [...firstCommit.childNodes]
33-
.map(node => interpretNode(node))
34-
.join('')
35-
.trim();
16+
const title = parseRenderedText(firstCommit.firstElementChild!, ({nodeName}) =>
17+
// Exclude expand body button
18+
nodeName === '#text' || nodeName === 'CODE' ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_REJECT,
19+
);
3620

3721
return {title, body};
3822
}
@@ -89,8 +73,14 @@ void features.add(import.meta.url, {
8973
});
9074

9175
/*
76+
9277
Test URLs
9378
94-
Few commit: https://github.com/refined-github/sandbox/compare/rendered-commit-title?expand=1
95-
Many commits: https://github.com/refined-github/refined-github/compare/refined-github:refined-github:esbuild-2...pgimalac:refined-github:pgimalac/fit-rendered-markdown?expand=1
79+
Few commits:
80+
- https://github.com/refined-github/sandbox/compare/rendered-commit-title?expand=1
81+
- https://github.com/refined-github/sandbox/compare/9012?expand=1
82+
Many commits:
83+
- https://github.com/refined-github/refined-github/compare/refined-github:refined-github:esbuild-2...pgimalac:refined-github:pgimalac/fit-rendered-markdown?expand=1
84+
- https://github.com/refined-github/sandbox/compare/9012-2?expand=1
85+
9686
*/

source/features/sync-pr-commit-title.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import observe from '../helpers/selector-observer.js';
1111
import cleanPrCommitTitle from '../helpers/pr-commit-cleaner.js';
1212
import setReactInputValue from '../helpers/set-react-input-value.js';
1313
import {confirmMergeButton} from '../github-helpers/selectors.js';
14+
import parseRenderedText from '../github-helpers/parse-rendered-text.js';
1415

1516
const commitTitleFieldSelector = '[data-testid="mergebox-partial"] input';
1617

@@ -33,7 +34,7 @@ function createCommitTitle(): string {
3334
// Old view - TODO: Remove after July 2026
3435
'input#issue_title',
3536
]);
36-
const prTitleText = (prTitle instanceof HTMLInputElement ? prTitle.value : prTitle.textContent).trim();
37+
const prTitleText = prTitle instanceof HTMLInputElement ? prTitle.value.trim() : parseRenderedText(prTitle);
3738
return formatPrCommitTitle(prTitleText);
3839
}
3940

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
export default function parseRenderedText(element: Element, filter?: NodeFilter): string {
2+
const walker = document.createTreeWalker(element, NodeFilter.SHOW_ALL, filter);
3+
4+
// eslint-disable-next-line @typescript-eslint/no-restricted-types
5+
let currentNode = walker.currentNode as Node | null;
6+
let parsedText = '';
7+
8+
while (currentNode) {
9+
if (currentNode.nodeName === 'CODE') {
10+
const {textContent} = currentNode;
11+
// Restore backticks that GitHub loses when rendering them
12+
parsedText += `\`${textContent?.trim()}\``;
13+
14+
// Skip the children
15+
currentNode = walker.nextSibling();
16+
while (!currentNode) {
17+
currentNode = walker.parentNode();
18+
19+
if (currentNode === walker.root) {
20+
return parsedText.trim();
21+
}
22+
23+
currentNode = walker.nextSibling();
24+
}
25+
26+
continue;
27+
}
28+
29+
if (currentNode.nodeType === Node.TEXT_NODE) {
30+
parsedText += currentNode.nodeValue;
31+
}
32+
33+
currentNode = walker.nextNode();
34+
}
35+
36+
return parsedText.trim();
37+
}

0 commit comments

Comments
 (0)