This repository has been archived by the owner on Jun 7, 2024. It is now read-only.
forked from speced/respec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
issues-notes.js
169 lines (169 loc) · 6 KB
/
issues-notes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Module core/issues-notes
// Manages issues and notes, including marking them up, numbering, inserting the title,
// and injecting the style sheet.
// These are elements with classes "issue" or "note".
// When an issue or note is found, it is reported using the "issue" or "note" event. This can
// be used by a containing shell to extract all of these.
// Issues are automatically numbered by default, but you can assign them specific numbers (or,
// despite the name, any arbitrary identifier) using the data-number attribute. Note that as
// soon as you use one data-number on any issue all the other issues stop being automatically
// numbered to avoid involuntary clashes.
// If the configuration has issueBase set to a non-empty string, and issues are
// manually numbered, a link to the issue is created using issueBase and the issue number
import { pub } from "core/pubsubhub";
import css from "deps/text!core/css/issues-notes.css";
import { fetch as ghFetch, fetchIndex } from "core/github";
export const name = "core/issues-notes";
export function run(conf, doc, cb) {
function handleIssues($ins, ghIssues, issueBase) {
$(doc).find("head link").first().before($("<style/>").text(css));
var hasDataNum = $(".issue[data-number]").length > 0,
issueNum = 0,
$issueSummary = $(
"<div><h2>" + conf.l10n.issue_summary + "</h2><ul></ul></div>"
),
$issueList = $issueSummary.find("ul");
$ins.each(function(i, inno) {
var $inno = $(inno),
isIssue = $inno.hasClass("issue"),
isWarning = $inno.hasClass("warning"),
isEdNote = $inno.hasClass("ednote"),
isFeatureAtRisk = $inno.hasClass("atrisk"),
isInline = $inno[0].localName === "span",
dataNum = $inno.attr("data-number"),
report = {
inline: isInline,
content: $inno.html(),
};
report.type = isIssue
? "issue"
: isWarning ? "warning" : isEdNote ? "ednote" : "note";
if (isIssue && !isInline && !hasDataNum) {
issueNum++;
report.number = issueNum;
} else if (dataNum) {
report.number = dataNum;
}
// wrap
if (!isInline) {
var $div = $(
"<div class='" +
report.type +
(isFeatureAtRisk ? " atrisk" : "") +
"'></div>"
),
$tit = $(
"<div role='heading' class='" + report.type + "-title'><span></span></div>"
),
text = isIssue
? isFeatureAtRisk ? "Feature at Risk" : conf.l10n.issue
: isWarning
? conf.l10n.warning
: isEdNote ? conf.l10n.editors_note : conf.l10n.note,
ghIssue;
$tit.makeID("h", report.type);
report.title = $inno.attr("title");
if (isIssue) {
if (hasDataNum) {
if (dataNum) {
text += " " + dataNum;
// Set issueBase to cause issue to be linked to the external issue tracker
if (!isFeatureAtRisk && issueBase) {
$tit
.find("span")
.wrap($("<a href='" + issueBase + dataNum + "'/>"));
} else if (isFeatureAtRisk && conf.atRiskBase) {
$tit
.find("span")
.wrap($("<a href='" + conf.atRiskBase + dataNum + "'/>"));
}
ghIssue = ghIssues[dataNum];
if (ghIssue && !report.title) {
report.title = ghIssue.title;
}
}
} else {
text += " " + issueNum;
}
if (report.number !== undefined) {
// Add entry to #issue-summary.
var id = "issue-" + report.number,
$li = $("<li><a></a></li>"),
$a = $li.find("a");
$div.attr("id", id);
$a
.attr("href", "#" + id)
.text(conf.l10n.issue + " " + report.number);
if (report.title) {
$li.append(
$(
"<span style='text-transform: none'>: " +
report.title +
"</span>"
)
);
}
$issueList.append($li);
}
}
$tit.find("span").text(text);
if (report.title) {
$tit.append(
$(
"<span style='text-transform: none'>: " + report.title + "</span>"
)
);
$inno.removeAttr("title");
}
$tit.addClass("marker");
$div.append($tit);
$inno.replaceWith($div);
var body = $inno.removeClass(report.type).removeAttr("data-number");
if (ghIssue && !body.text().trim()) {
body = ghIssue.body_html;
}
$div.append(body);
const level = $tit.parents("section").length + 2;
$tit.attr("aria-level", level);
}
pub(report.type, report);
});
if ($(".issue").length) {
if ($("#issue-summary"))
$("#issue-summary").append($issueSummary.contents());
} else if ($("#issue-summary").length) {
pub("warn", "Using issue summary (#issue-summary) but no issues found.");
$("#issue-summary").remove();
}
}
var $ins = $(".issue, .note, .warning, .ednote"),
ghIssues = {},
issueBase = conf.issueBase;
if ($ins.length) {
if (conf.githubAPI) {
ghFetch(conf.githubAPI)
.then(function(json) {
issueBase = issueBase || json.html_url + "/issues/";
return fetchIndex(json.issues_url, {
// Get back HTML content instead of markdown
// See: https://developer.github.com/v3/media/
headers: {
Accept: "application/vnd.github.v3.html+json",
},
});
})
.then(function(issues) {
issues.forEach(function(issue) {
ghIssues[issue.number] = issue;
});
handleIssues($ins, ghIssues, issueBase);
cb();
});
} else {
handleIssues($ins, ghIssues, issueBase);
cb();
}
} else {
cb();
}
}