Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
"dom-loaded": "^1.0.0",
"element-ready": "^2.0.0",
"github-injection": "^0.3.0",
"linkify-issues": "^1.1.0",
"linkify-urls": "^1.0.2",
"linkify-issues": "^1.3.0",
"linkify-urls": "^1.3.0",
"select-dom": "^4.1.0",
"shorten-repo-url": "^1.1.0",
"to-markdown": "^3.0.4",
Expand Down
10 changes: 8 additions & 2 deletions src/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import linkifyCode, {editTextNodes} from './libs/linkify-urls-in-code';
import autoLoadMoreNews from './libs/auto-load-more-news';
import * as icons from './libs/icons';
import * as pageDetect from './libs/page-detect';
import {getUsername} from './libs/utils';
import {getUsername, observeEl} from './libs/utils';

// Add globals for easier debugging
window.$ = $;
Expand Down Expand Up @@ -241,7 +241,13 @@ function addDeleteForkLink() {
}

function linkifyIssuesInTitles() {
editTextNodes(linkifyIssues, select('.js-issue-title'));
observeEl(select('#partial-discussion-header').parentNode, () => {
const title = select('.js-issue-title:not(.refined-linkified-title)');
if (title) {
title.classList.add('refined-linkified-title');
editTextNodes(linkifyIssues, title);
}
});
}

function addPatchDiffLinks() {
Expand Down
33 changes: 12 additions & 21 deletions src/libs/linkify-urls-in-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@ import linkifyUrls from 'linkify-urls';
import linkifyIssues from 'linkify-issues';
import {getOwnerAndRepo} from './page-detect';
import getTextNodes from './get-text-nodes';
import html from './domify';

// Necessary because textNodes don't have .innerHTML
const getInnerHTML = textNode => {
const div = document.createElement('div');
div.textContent = textNode.textContent;
return div.innerHTML;
};

const linkifiedURLClass = 'refined-github-linkified-code';
const {
Expand All @@ -21,47 +13,46 @@ const {
const options = {
user: ownerName,
repo: repoName,
type: 'dom',
attrs: {
target: '_blank'
}
};

export const editTextNodes = (fn, el) => {
if (!el) {
return;
}
for (const textNode of getTextNodes(el)) {
if (textNode.textContent.length < 11) { // Shortest url: http://j.mp
if (fn === linkifyUrls && textNode.textContent.length < 11) { // Shortest url: http://j.mp
continue;
}
const textHTML = getInnerHTML(textNode);
const linkified = fn(textHTML, options);
if (linkified !== textHTML) {
textNode.replaceWith(html(linkified));
const linkified = fn(textNode.textContent, options);
if (linkified.children.length > 0) { // Children are <a>
textNode.replaceWith(linkified);
}
}
};

export default () => {
const untouchedCode = select.all(`.blob-wrapper:not(.${linkifiedURLClass})`);
const wrappers = select.all(`.highlight:not(.${linkifiedURLClass})`);

// Don't linkify any already linkified code
if (untouchedCode.length === 0) {
if (wrappers.length === 0) {
return;
}

// Linkify full URLs
for (const el of select.all('.blob-code-inner', untouchedCode)) {
// `.blob-code-inner` in diffs
// `pre` in GitHub comments
for (const el of select.all('.blob-code-inner, pre', wrappers)) {
editTextNodes(linkifyUrls, el);
}

// Linkify issue refs in comments
for (const el of select.all('.blob-code-inner span.pl-c', untouchedCode)) {
for (const el of select.all('span.pl-c', wrappers)) {
editTextNodes(linkifyIssues, el);
}

// Mark code block as touched
for (const el of untouchedCode) {
for (const el of wrappers) {
el.classList.add(linkifiedURLClass);
}
};
2 changes: 1 addition & 1 deletion src/libs/page-detect.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const isCommit = () => isSingleCommit() || isPRCommit() || (isPRFiles() &

export const isCompare = () => isRepo() && /^\/compare/.test(getRepoPath());

export const hasCode = () => isRepo() && select.exists('.blob-code-inner');
export const hasCode = () => isRepo() && select.exists('.highlight');

export const hasDiff = () => isRepo() && (isSingleCommit() || isPRCommit() || isPRFiles() || isCompare() || (isPR() && select.exists('.diff-table')));

Expand Down
12 changes: 12 additions & 0 deletions src/libs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,15 @@ export const emptyElement = element => {
element.firstChild.remove();
}
};

export const observeEl = (el, listener, options) => {
options = Object.assign({
childList: true
}, options);

// Run first
listener();

// Run on updates
return new MutationObserver(listener).observe(el, options);
};