Skip to content

Commit d2c5f44

Browse files
committed
Merge branch 'master' into bundle-with-webpack
2 parents b94d583 + 0407f00 commit d2c5f44

File tree

1 file changed

+85
-31
lines changed

1 file changed

+85
-31
lines changed

ext/src/content/content.js

Lines changed: 85 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ function init() {
2626

2727
const GITHUB_URL = "https://github.com";
2828
const GITHUB_VALID_PATHNAMES = /^\/.*\/.*\/(?:pull\/\d+(?:\/?|\/files\/?)$|commit|compare\/.*|issues\/\d+|issues\/new)/u;
29+
const POLLING_INTERVAL = 30;
2930
let isGithubListenerAdded = false;
3031

3132
const STACKOVERFLOW_URL = "https://stackoverflow.com";
@@ -213,6 +214,7 @@ function init() {
213214
const codeBlocks = inputEl.value.split("\n").reduce((groups, line) => {
214215
const codeBlockRegex = /^\s{0,3}(?:```|~~~)/u;
215216
const indentedCodeLangRegex = /^\s*<!-- language: lang-.* -->/u;
217+
const langAllRegex = /^\s*<!-- language-all: lang-.+ -->/u;
216218
const emptyLineRegex = /^\s*$/u;
217219
const indentedCodeBlockRegex = /^\s{4}/u;
218220
const codeSnippetRegex = /`{1,2}[^\n`]+`{1,2}/u;
@@ -248,7 +250,7 @@ function init() {
248250
* ``const foo = `${bar}`;``
249251
*/
250252
} else if (codeSnippetRegex.test(line)) {
251-
groups.push(line);
253+
groups.push([line]);
252254

253255
/*
254256
* Code blocks using indented lines:
@@ -280,6 +282,13 @@ function init() {
280282
} else {
281283
groups.push([line]);
282284
}
285+
/*
286+
* language-all comments:
287+
*
288+
* <!-- language-all: lang-js -->
289+
*/
290+
} else if (langAllRegex.test(line)) {
291+
groups.push([line]);
283292
}
284293

285294
return groups;
@@ -291,50 +300,95 @@ function init() {
291300
}
292301

293302
if (codeBlocks.length) {
294-
/*
295-
* TODO: Add support for language-all: <!-- language-all: lang-* -->
296-
* Once this support is added, we can format inline code snippets (i.e. `const foo = 'bar';`).
297-
* https://stackoverflow.com/editing-help#syntax-highlighting
298-
*/
303+
let langAll = null;
304+
305+
// https://stackoverflow.com/editing-help#syntax-highlighting
299306
codeBlocks.forEach(lines => {
300-
const codeBlockRegex = /^\s{0,3}(?:```|~~~)\s*lang-(.+)/u;
301-
const indentedCodeRegex = /^\s*<!-- language: lang-(.+) -->/u;
307+
const codeBlockRegex = /^\s{0,3}(?:```|~~~)\s*(?:lang-(.+))?/u;
308+
const indentedCodeWithLangRegex = /^\s*<!-- language: lang-(.+) -->/u;
309+
const langAllRegex = /^\s*<!-- language-all: lang-(.+) -->/u;
310+
const codeSnippetRegex = /(`{1,2})([^\n`]+)(`{1,2})/gu;
302311
const [firstLine] = lines;
303-
const [, lang = null] =
312+
313+
if (langAllRegex.test(firstLine)) {
314+
[, langAll = null] = firstLine.match(langAllRegex);
315+
return;
316+
}
317+
318+
const [, lang = langAll] =
304319
firstLine.match(codeBlockRegex) ||
305-
firstLine.match(indentedCodeRegex) ||
320+
firstLine.match(indentedCodeWithLangRegex) ||
306321
[];
307322

308323
if (!lang) {
309324
return;
310325
}
311326

312-
const isCodeBlock = codeBlockRegex.test(firstLine);
313-
const indentedLineCodeBlockStartIdx = 2;
314-
const codeLines = isCodeBlock
315-
? lines.slice(1, -1)
316-
: lines.slice(indentedLineCodeBlockStartIdx);
317-
let formattedBlock = prettier.format(codeLines.join("\n"), {
318-
parser: PARSERS_LANG_MAP[lang],
319-
plugins: prettierPlugins
320-
});
321-
322-
// Prettier adds a trailing newline
323-
if (codeLines.length !== formattedBlock.split("\n").length) {
324-
formattedBlock = formattedBlock.replace(/\n$/u, "");
325-
}
327+
let formattedText = lines.join("\n");
328+
329+
// Code Snippets
330+
if (codeSnippetRegex.test(firstLine)) {
331+
formattedText = firstLine.replace(
332+
codeSnippetRegex,
333+
(match, openingBackticks, snippet, closingBackticks) => {
334+
let formattedSnippet = snippet;
335+
336+
try {
337+
formattedSnippet = prettier.format(snippet, {
338+
parser: PARSERS_LANG_MAP[lang],
339+
plugins: prettierPlugins
340+
});
341+
} catch {}
326342

327-
formattedBlock = isCodeBlock
328-
? `${firstLine}\n${formattedBlock}\n${lines[lines.length - 1]}`
329-
: `${firstLine}\n${lines[1]}\n ${
330-
formattedBlock.split("\n").length > 1
331-
? formattedBlock.split("\n").join("\n ")
332-
: formattedBlock
343+
return `${openingBackticks}${formattedSnippet}${closingBackticks}`;
344+
}
345+
);
346+
347+
// Code Blocks
348+
} else {
349+
const isCodeBlock = codeBlockRegex.test(firstLine);
350+
const isIndentedBlockWithLang = indentedCodeWithLangRegex.test(
351+
firstLine
352+
);
353+
let codeLines;
354+
355+
if (isCodeBlock) {
356+
codeLines = codeLines.slice(1, -1);
357+
} else {
358+
const indentedLineCodeBlockStartIdx = 2;
359+
codeLines = isIndentedBlockWithLang
360+
? lines.slice(indentedLineCodeBlockStartIdx)
361+
: lines;
362+
}
363+
364+
try {
365+
formattedText = prettier.format(codeLines.join("\n"), {
366+
parser: PARSERS_LANG_MAP[lang],
367+
plugins: prettierPlugins
368+
});
369+
} catch {
370+
return;
371+
}
372+
373+
if (isCodeBlock) {
374+
formattedText = `${firstLine}\n${formattedText}\n${
375+
lines[lines.length - 1]
333376
}`;
377+
} else {
378+
const langComment = isIndentedBlockWithLang
379+
? `${firstLine}\n${lines[1]}\n`
380+
: "";
381+
formattedText = `${langComment} ${
382+
formattedText.split("\n").length > 1
383+
? formattedText.split("\n").join("\n ")
384+
: formattedText
385+
}`;
386+
}
387+
}
334388

335389
inputEl.value = inputEl.value.replace(
336390
lines.join("\n"),
337-
formattedBlock
391+
formattedText
338392
);
339393
});
340394
}

0 commit comments

Comments
 (0)