From 38bd3b0fc52586de7d0aba7bbbfc3e5dc100f979 Mon Sep 17 00:00:00 2001 From: David Bolack Date: Tue, 11 Feb 2025 14:34:01 -0600 Subject: [PATCH] Migrate the justified paragraphs extension to an NPM --- package-lock.json | 10 +++++ package.json | 1 + shared/naturalcrit/markdown.js | 40 ++----------------- .../markdown/paragraph-justification.test.js | 27 ------------- 4 files changed, 14 insertions(+), 64 deletions(-) delete mode 100644 tests/markdown/paragraph-justification.test.js diff --git a/package-lock.json b/package-lock.json index bd3f71491..a57084dc6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -38,6 +38,7 @@ "marked-emoji": "^1.4.3", "marked-extended-tables": "^1.1.0", "marked-gfm-heading-id": "^4.0.1", + "marked-justified-paragraphs": "^1.0.0", "marked-smartypants-lite": "^1.0.3", "markedLegacy": "npm:marked@^0.3.19", "moment": "^2.30.1", @@ -9874,6 +9875,15 @@ "marked": ">=13 <16" } }, + "node_modules/marked-justified-paragraphs": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/marked-justified-paragraphs/-/marked-justified-paragraphs-1.0.0.tgz", + "integrity": "sha512-TgTKij4HbYy85zWAZ0Va7JCOU/yh8d12Jq2J/jaBHNMa6gJDAsbLT42MFLU9gwLYxsg8hCJHJ0n0zYY6zo8jiA==", + "license": "MIT", + "peerDependencies": { + "marked": ">=3 <16" + } + }, "node_modules/marked-smartypants-lite": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/marked-smartypants-lite/-/marked-smartypants-lite-1.0.3.tgz", diff --git a/package.json b/package.json index 342af4d6e..8c343833d 100644 --- a/package.json +++ b/package.json @@ -113,6 +113,7 @@ "marked-emoji": "^1.4.3", "marked-extended-tables": "^1.1.0", "marked-gfm-heading-id": "^4.0.1", + "marked-justified-paragraphs": "^1.0.0", "marked-smartypants-lite": "^1.0.3", "markedLegacy": "npm:marked@^0.3.19", "moment": "^2.30.1", diff --git a/shared/naturalcrit/markdown.js b/shared/naturalcrit/markdown.js index 99766b536..e9ea49d46 100644 --- a/shared/naturalcrit/markdown.js +++ b/shared/naturalcrit/markdown.js @@ -7,6 +7,7 @@ import MarkedExtendedTables from 'marked-extended-tables'; import { markedSmartypantsLite as MarkedSmartypantsLite } from 'marked-smartypants-lite'; import { gfmHeadingId as MarkedGFMHeadingId, resetHeadings as MarkedGFMResetHeadingIDs } from 'marked-gfm-heading-id'; import { markedEmoji as MarkedEmojis } from 'marked-emoji'; +import MarkedJustifiedParagraphs from 'marked-justified-paragraphs'; //Icon fonts included so they can appear in emoji autosuggest dropdown import diceFont from '../../themes/fonts/iconFonts/diceFont.js'; @@ -362,42 +363,6 @@ const superSubScripts = { }; -const justifiedParagraphClasses = []; -justifiedParagraphClasses[2] = 'Left'; -justifiedParagraphClasses[4] = 'Right'; -justifiedParagraphClasses[6] = 'Center'; - -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(([^\n].*\n)*(\n|$))|$)/ygm; - 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 + 1)) - }; - } - }, - renderer(token) { - return `

${this.parser.parseInline(token.tokens)}

`; - } - -}; - - const forcedParagraphBreaks = { name : 'hardBreaks', level : 'block', @@ -795,8 +760,9 @@ const tableTerminators = [ ]; Marked.use(MarkedVariables()); -Marked.use({ extensions : [justifiedParagraphs, definitionListsMultiLine, definitionListsSingleLine, forcedParagraphBreaks, +Marked.use({ extensions : [definitionListsMultiLine, definitionListsSingleLine, forcedParagraphBreaks, nonbreakingSpaces, superSubScripts, mustacheSpans, mustacheDivs, mustacheInjectInline] }); +Marked.use(MarkedJustifiedParagraphs()); Marked.use(mustacheInjectBlock); Marked.use({ renderer: renderer, tokenizer: tokenizer, mangle: false }); Marked.use(MarkedExtendedTables(tableTerminators), MarkedGFMHeadingId({ globalSlugs: true }), diff --git a/tests/markdown/paragraph-justification.test.js b/tests/markdown/paragraph-justification.test.js deleted file mode 100644 index 48b311e85..000000000 --- a/tests/markdown/paragraph-justification.test.js +++ /dev/null @@ -1,27 +0,0 @@ -/* 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(`

Hello

`); - }); - test('Right Justify', function() { - const source = '-: Hello'; - const rendered = Markdown.render(source); - expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe(`

Hello

`); - }); - test('Center Justify', function() { - const source = ':-: Hello'; - const rendered = Markdown.render(source); - expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe(`

Hello

`); - }); - - 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(`
\n:- Hello\n
\n`); - }); -});