Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V4 proposed aligned paragraph tokens #3908

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
40 changes: 39 additions & 1 deletion shared/naturalcrit/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,44 @@ const superSubScripts = {
}
};


const justifiedParagraphClasses = [];
justifiedParagraphClasses[2] = 'mdParagraphJustifyLeft';
justifiedParagraphClasses[4] = 'mdParagraphJustifyRight';
justifiedParagraphClasses[6] = 'mdParagraphJustifyCenter';

const justifiedParagraphs = {
name : 'justifiedParagraphs',
level : 'block',
start(src) {
return src.match(/\n(?:-:|:-|:-:) {1}/m)?.index;

}, // Hint to Marked.js to stop and check for a match
tokenizer(src, tokens) {
const regex = /^((:- ).*)|((-: ).*)|((:-: ).*)(?:\n|$)/ym;
const match = regex.exec(src);
if(match?.length) {
let whichJustify;
if(match[2]?.length) whichJustify = 2;
if(match[4]?.length) whichJustify = 4;
if(match[6]?.length) whichJustify = 6;
return {
type : 'justifiedParagraphs', // Should match "name" above
raw : match[0], // Text to consume from the source
length : match[whichJustify].length,
text : match[0].slice(match[whichJustify].length),
class : justifiedParagraphClasses[whichJustify],
tokens : this.lexer.inlineTokens(match[0].slice(match[whichJustify].length))
};
}
},
renderer(token) {
return `<p class="${token.class}">${this.parser.parseInline(token.tokens)}</p>`;
}

};


const forcedParagraphBreaks = {
name : 'hardBreaks',
level : 'block',
Expand Down Expand Up @@ -748,7 +786,7 @@ const tableTerminators = [
];

Marked.use(MarkedVariables());
Marked.use({ extensions : [definitionListsMultiLine, definitionListsSingleLine, forcedParagraphBreaks, superSubScripts,
Marked.use({ extensions : [justifiedParagraphs, definitionListsMultiLine, definitionListsSingleLine, forcedParagraphBreaks, superSubScripts,
mustacheSpans, mustacheDivs, mustacheInjectInline] });
Marked.use(mustacheInjectBlock);
Marked.use({ renderer: renderer, tokenizer: tokenizer, mangle: false });
Expand Down
27 changes: 27 additions & 0 deletions tests/markdown/justification.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* eslint-disable max-lines */

import Markdown from 'naturalcrit/markdown.js';

describe('Justification', ()=>{
test('Left Justify', function() {
const source = ':- Hello';
const rendered = Markdown.render(source);
expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe(`<p class=\"mdParagraphJustifyLeft\">Hello</p>`);
});
test('Right Justify', function() {
const source = '-: Hello';
const rendered = Markdown.render(source);
expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe(`<p class=\"mdParagraphJustifyRight\">Hello</p>`);
});
test('Center Justify', function() {
const source = ':-: Hello';
const rendered = Markdown.render(source);
expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe(`<p class=\"mdParagraphJustifyCenter\">Hello</p>`);
});

test('Ignored inside a code block', function() {
const source = '```\n\n:- Hello\n\n```\n';
const rendered = Markdown.render(source);
expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe(`<pre><code>\n:- Hello\n</code></pre>\n`);
});
});
21 changes: 20 additions & 1 deletion themes/V3/Blank/style.less
Original file line number Diff line number Diff line change
Expand Up @@ -503,4 +503,23 @@ body { counter-reset : page-numbers 0; }
counter-increment : page-numbers;
}

}
}

//*****************************
//* Forced Justification
//* DELETE WHEN V4 GOES LIVE
//*****************************/

.page {
.mdParagraphJustifyLeft {
text-align: left;
}

.mdParagraphJustifyRight {
text-align: right;
}

.mdParagraphJustifyCenter {
text-align: center;
}
}
Binary file added themes/V4/Blank/dropdownPreview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added themes/V4/Blank/dropdownTexture.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions themes/V4/Blank/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name" : "Blank",
"renderer" : "V3",
"baseTheme" : false,
"baseSnippets" : false
}
Loading