Skip to content
Open
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
42 changes: 27 additions & 15 deletions lib/links.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as cheerio from 'cheerio';
import { Parser } from 'htmlparser2';

const FIXES_RE = /(Close[ds]?|Fix(e[ds])?|Resolve[sd]?)\s*:\s*(\S+)/mgi;
const FIX_RE = /(Close[ds]?|Fix(e[ds])?|Resolve[sd]?)\s*:\s*(\S+)/i;
Expand All @@ -13,7 +13,26 @@ export class LinkParser {
constructor(owner, repo, html) {
this.owner = owner;
this.repo = repo;
this.$ = cheerio.load(html);
this.text = '';
this.links = [];

let currentLink;
const parser = new Parser({
onopentag: (name, attributes) => {
if (name === 'a') {
currentLink = { href: attributes.href, text: '' };
this.links.push(currentLink);
}
},
ontext: (text) => {
this.text += text;
if (currentLink) currentLink.text += text;
},
onclosetag: (name) => {
if (name === 'a') currentLink = undefined;
}
});
parser.end(html);
}

getFixesUrlsFromArray(arr) {
Expand Down Expand Up @@ -55,32 +74,25 @@ export class LinkParser {
// Do this so we can reliably get the correct url.
// Otherwise, the number could reference a PR or an issue.
getUrlFromOP(ref) {
const as = this.$('a');
const links = as.map((i, el) => this.$(el)).get();
for (const link of links) {
const text = link.text();
if (text === ref) {
const href = link.attr('href');
if (href) return href;
for (const link of this.links) {
if (link.text === ref) {
if (link.href) return link.href;
}
}
}

getFixes() {
const text = this.$.text();
const fixes = text.match(FIXES_RE) || [];
const fixes = this.text.match(FIXES_RE) || [];
return this.getFixesUrlsFromArray(fixes);
}

getRefs() {
const text = this.$.text();
const refs = text.match(REFS_RE) || [];
const refs = this.text.match(REFS_RE) || [];
return this.getRefsUrlsFromArray(refs);
}

getAltPrUrl() {
const text = this.$.text();
const refs = text.match(PR_RE) || [];
const refs = this.text.match(PR_RE) || [];
return this.getPRUrlsFromArray(refs);
}
}
Expand Down
Loading
Loading