Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Commit

Permalink
DEV: apply coding standards (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
jjaffeux committed Sep 4, 2020
1 parent 046f4a0 commit 56cb458
Show file tree
Hide file tree
Showing 8 changed files with 1,637 additions and 24 deletions.
3 changes: 3 additions & 0 deletions .eslintrc
@@ -0,0 +1,3 @@
{
"extends": "eslint-config-discourse"
}
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -6,3 +6,6 @@ pkg/
auto_generated
.DS_Store
*.swp
node_modules
yarn-error.log
.rubocop-https---raw-githubusercontent-com-discourse-*
4 changes: 4 additions & 0 deletions .template-lintrc.js
@@ -0,0 +1,4 @@
module.exports = {
plugins: ["ember-template-lint-plugin-discourse"],
extends: "discourse:recommended",
};
23 changes: 12 additions & 11 deletions assets/javascripts/discourse/initializers/checklist.js.es6
@@ -1,6 +1,7 @@
import { withPluginApi } from "discourse/lib/plugin-api";
import { ajax } from "discourse/lib/ajax";
import { iconHTML } from "discourse-common/lib/icon-library";
import I18n from "I18n";

function initializePlugin(api) {
const siteSettings = api.container.lookup("site-settings:main");
Expand All @@ -24,7 +25,7 @@ export function checklistSyntax($elem, postDecorator) {
}

$boxes.each((idx, val) => {
$(val).click(ev => {
$(val).click((ev) => {
const $box = $(ev.currentTarget);

if ($box.hasClass("permanent")) return;
Expand All @@ -34,7 +35,7 @@ export function checklistSyntax($elem, postDecorator) {
$box.after(iconHTML("spinner", { class: "fa-spin" })).hide();

ajax(`/posts/${postModel.id}`, { type: "GET", cache: false }).then(
result => {
(result) => {
const blocks = [];

// Computing offsets where checkbox are not evaluated (i.e. inside
Expand All @@ -49,8 +50,8 @@ export function checklistSyntax($elem, postDecorator) {
// italic/bold
/_(?=\S).*?\S_/gm,
// strikethrough
/~~(?=\S).*?\S~~/gm
].forEach(regex => {
/~~(?=\S).*?\S~~/gm,
].forEach((regex) => {
let match;
while ((match = regex.exec(result.raw)) != null) {
blocks.push([match.index, match.index + match[0].length]);
Expand All @@ -59,8 +60,8 @@ export function checklistSyntax($elem, postDecorator) {

[
// italic/bold
/([^\[\n]|^)\*\S.+?\S\*(?=[^\]\n]|$)/gm
].forEach(regex => {
/([^\[\n]|^)\*\S.+?\S\*(?=[^\]\n]|$)/gm,
].forEach((regex) => {
let match;
while ((match = regex.exec(result.raw)) != null) {
// Simulate lookbehind - skip the first character
Expand All @@ -79,7 +80,7 @@ export function checklistSyntax($elem, postDecorator) {
}

nth += blocks.every(
b => b[0] >= off + match.length || off > b[1]
(b) => b[0] >= off + match.length || off > b[1]
);

if (nth === idx) {
Expand All @@ -93,7 +94,7 @@ export function checklistSyntax($elem, postDecorator) {

const save = postModel.save({
raw: newRaw,
edit_reason: I18n.t("checklist.edit_reason")
edit_reason: I18n.t("checklist.edit_reason"),
});

if (save && save.then) {
Expand All @@ -113,7 +114,7 @@ export function checklistSyntax($elem, postDecorator) {

export default {
name: "checklist",
initialize: function() {
withPluginApi("0.1", api => initializePlugin(api));
}
initialize: function () {
withPluginApi("0.1", (api) => initializePlugin(api));
},
};
4 changes: 2 additions & 2 deletions assets/javascripts/lib/discourse-markdown/checklist.js.es6
Expand Up @@ -95,10 +95,10 @@ export function setup(helper) {
"span.chcklst-stroked",
"span.chcklst-box fa fa-square-o fa-fw",
"span.chcklst-box checked fa fa-check-square-o fa-fw",
"span.chcklst-box checked permanent fa fa-check-square fa-fw"
"span.chcklst-box checked permanent fa fa-check-square fa-fw",
]);

helper.registerPlugin(md =>
helper.registerPlugin((md) =>
md.core.ruler.push("checklist", processChecklist)
);
}
7 changes: 7 additions & 0 deletions package.json
@@ -0,0 +1,7 @@
{
"author": "Discourse",
"license": "MIT",
"devDependencies": {
"eslint-config-discourse": "latest"
}
}
22 changes: 11 additions & 11 deletions test/javascripts/lib/checklist-test.js.es6
Expand Up @@ -7,7 +7,7 @@ QUnit.module("initializer:checklist");

async function prepare(raw) {
const cooked = await cookAsync(raw, {
siteSettings: { checklist_enabled: true }
siteSettings: { checklist_enabled: true },
});
const model = Post.create({ id: 42, can_edit: true });
const decoratorHelper = { getModel: () => model };
Expand All @@ -16,20 +16,20 @@ async function prepare(raw) {
server.get("/posts/42", () => [
200,
{ "Content-Type": "application/json" },
{ raw }
{ raw },
]);

const $elem = $(cooked.string);
checklistSyntax($elem, decoratorHelper);

const updated = new Promise(resolve => {
model.save = fields => resolve(fields.raw);
const updated = new Promise((resolve) => {
model.save = (fields) => resolve(fields.raw);
});

return [$elem, updated];
}

QUnit.test("checkbox before a code block", async assert => {
QUnit.test("checkbox before a code block", async (assert) => {
const [$elem, updated] = await prepare(`
[ ] first
[x] actual
Expand All @@ -45,7 +45,7 @@ QUnit.test("checkbox before a code block", async assert => {
assert.ok(output.includes("[x] nope"));
});

QUnit.test("permanently checked checkbox", async assert => {
QUnit.test("permanently checked checkbox", async (assert) => {
const [$elem, updated] = await prepare(`
[X] perma
[x] not perma
Expand All @@ -60,7 +60,7 @@ QUnit.test("permanently checked checkbox", async assert => {
assert.ok(output.includes("[ ] not perma"));
});

QUnit.test("checkbox before a multiline code block", async assert => {
QUnit.test("checkbox before a multiline code block", async (assert) => {
const [$elem, updated] = await prepare(`
[ ] first
[x] actual
Expand All @@ -79,7 +79,7 @@ QUnit.test("checkbox before a multiline code block", async assert => {
assert.ok(output.includes("[x] nope"));
});

QUnit.test("checkbox before italic/bold sequence", async assert => {
QUnit.test("checkbox before italic/bold sequence", async (assert) => {
const [$elem, updated] = await prepare(` [x] *test*
`);

Expand All @@ -90,7 +90,7 @@ QUnit.test("checkbox before italic/bold sequence", async assert => {
assert.ok(output.includes("[ ] *test*"));
});

QUnit.test("checkboxes in an unordered list", async assert => {
QUnit.test("checkboxes in an unordered list", async (assert) => {
const [$elem, updated] = await prepare(`
* [x] checked
* [] test
Expand All @@ -106,7 +106,7 @@ QUnit.test("checkboxes in an unordered list", async assert => {
assert.ok(output.includes("* [] two"));
});

QUnit.test("checkboxes in italic/bold-like blocks", async assert => {
QUnit.test("checkboxes in italic/bold-like blocks", async (assert) => {
const [$elem, updated] = await prepare(`
*[x
*a [*] x]*
Expand All @@ -129,7 +129,7 @@ QUnit.test("checkboxes in italic/bold-like blocks", async assert => {
assert.ok(output.includes("* [ ] 3"));
});

QUnit.test("correct checkbox is selected", async assert => {
QUnit.test("correct checkbox is selected", async (assert) => {
const [$elem, updated] = await prepare(`
\`[x]\`
*[x]*
Expand Down

0 comments on commit 56cb458

Please sign in to comment.