Parcourir la source

Adding classes to some rendered nodes. Playground sample typos.

main
Rocketsoup il y a 1 an
Parent
révision
2f8bc87783
4 fichiers modifiés avec 44 ajouts et 19 suppressions
  1. 37
    11
      js/markdown.js
  2. 1
    1
      js/markdown.min.js
  3. 2
    2
      markdownjs.html
  4. 4
    5
      testjs.html

+ 37
- 11
js/markdown.js Voir le fichier

@@ -1557,7 +1557,9 @@ class MDTagModifier {
1557 1557
 	 */
1558 1558
 	applyTo(node) {
1559 1559
 		if (node instanceof MDNode) {
1560
-			node.cssClasses = node.cssClasses.concat(this.cssClasses);
1560
+			for (const cssClass of this.cssClasses) {
1561
+				node.addClass(cssClass);
1562
+			}
1561 1563
 			if (this.cssId) node.cssId = this.cssId;
1562 1564
 			for (const name in this.attributes) {
1563 1565
 				node.attributes[name] = this.attributes[name];
@@ -1575,7 +1577,7 @@ class MDTagModifier {
1575 1577
 	 * @returns {boolean} whether the class was added
1576 1578
 	 */
1577 1579
 	addClass(cssClass) {
1578
-		if (this.cssClasses.contains(cssClass)) return false;
1580
+		if (this.cssClasses.indexOf(cssClass) >= 0) return false;
1579 1581
 		this.cssClasses.push(cssClass);
1580 1582
 		return true;
1581 1583
 	}
@@ -1623,7 +1625,7 @@ class MDTagModifier {
1623 1625
 		for (const token of modifierTokens) {
1624 1626
 			if (token.trim() == '') continue;
1625 1627
 			if (groups = this.#classRegex.exec(token)) {
1626
-				mod.cssClasses.push(groups[1]);
1628
+				mod.addClass(groups[1]);
1627 1629
 			} else if (groups = this.#idRegex.exec(token)) {
1628 1630
 				mod.cssId = groups[1];
1629 1631
 			} else if (groups = this.#attributeRegex.exec(token)) {
@@ -3188,6 +3190,30 @@ class MDNode {
3188 3190
 	}
3189 3191
 
3190 3192
 	/**
3193
+	 * Adds a CSS class. If already present it will not be duplicated.
3194
+	 *
3195
+	 * @param {string} cssClass
3196
+	 * @returns {boolean} whether the class was added
3197
+	 */
3198
+	addClass(cssClass) {
3199
+		if (this.cssClasses.indexOf(cssClass) >= 0) return false;
3200
+		this.cssClasses.push(cssClass);
3201
+		return true;
3202
+	}
3203
+
3204
+	/**
3205
+	 * Removes a CSS class.
3206
+	 *
3207
+	 * @param {string} cssClass
3208
+	 * @returns {boolean} whether the class was present and removed
3209
+	 */
3210
+	removeClass(cssClass) {
3211
+		const beforeLength = this.cssClasses.length;
3212
+		this.cssClasses = this.cssClasses.filter((val) => val !== cssClass);
3213
+		return this.cssClasses.length != beforeLength;
3214
+	}
3215
+
3216
+	/**
3191 3217
 	 * Renders this node and any children as an HTML string. If the node has no
3192 3218
 	 * content an empty string should be returned.
3193 3219
 	 *
@@ -3383,9 +3409,7 @@ class MDHeadingNode extends MDBlockNode {
3383 3409
  */
3384 3410
 class MDSubtextNode extends MDBlockNode {
3385 3411
 	toHTML(state) {
3386
-		if (this.cssClasses.indexOf('subtext') < 0) {
3387
-			this.cssClasses.push('subtext');
3388
-		}
3412
+		this.addClass('subtext');
3389 3413
 		return this._simplePairedTagHTML(state, 'div');
3390 3414
 	}
3391 3415
 }
@@ -3670,7 +3694,7 @@ class MDFootnoteListNode extends MDBlockNode {
3670 3694
 		if (Object.keys(footnotes).length == 0) return '';
3671 3695
 		const footnoteUniques = state.root.footnoteInstances;
3672 3696
 		var html = '';
3673
-		html += '<div class="footnotes"><hr/>';
3697
+		html += '<div class="footnotes">';
3674 3698
 		html += '<ol>';
3675 3699
 		for (const symbol of symbolOrder) {
3676 3700
 			/** @type {MDNode[]} */
@@ -3678,7 +3702,7 @@ class MDFootnoteListNode extends MDBlockNode {
3678 3702
 			if (!content) continue;
3679 3703
 			let footnoteId = this.#footnoteId(state, symbol);
3680 3704
 			const contentHTML = MDNode.toHTML(content, state);
3681
-			html += `<li value="${symbol}" id="${state.root.elementIdPrefix}footnote_${footnoteId}">${contentHTML}`;
3705
+			html += `<li value="${footnoteId}" id="${state.root.elementIdPrefix}footnote_${footnoteId}">${contentHTML}`;
3682 3706
 			const uniques = footnoteUniques[symbol];
3683 3707
 			if (uniques) {
3684 3708
 				for (const unique of uniques) {
@@ -3865,7 +3889,7 @@ class MDFootnoteNode extends MDInlineNode {
3865 3889
 
3866 3890
 	toHTML(state) {
3867 3891
 		if (this.differentiator !== null) {
3868
-			return `<sup id="${state.root.elementIdPrefix}footnoteref_${this.occurrenceId}"${this._htmlAttributes()}>` +
3892
+			return `<sup class="footnote" id="${state.root.elementIdPrefix}footnoteref_${this.occurrenceId}"${this._htmlAttributes()}>` +
3869 3893
 				`<a href="#${state.root.elementIdPrefix}footnote_${this.footnoteId}">${MDUtils.escapeHTML(this.displaySymbol ?? this.symbol)}</a></sup>`;
3870 3894
 		}
3871 3895
 		return `<!--FNREF:{${this.symbol}}-->`;
@@ -3972,8 +3996,10 @@ class MDReferencedImageNode extends MDImageNode {
3972 3996
 
3973 3997
 	toHTML(state) {
3974 3998
 		if (this.src === '') {
3975
-			this.src = state.urlForReference(this.reference);
3976
-			this.attributes['title'] = state.urlTitleForReference(this.reference);
3999
+			const url = state.urlForReference(this.reference);
4000
+			if (url !== null) this.src = url;
4001
+			const title = state.urlTitleForReference(this.reference);
4002
+			if (title !== null) this.attributes['title'] = title;
3977 4003
 		}
3978 4004
 		return super.toHTML(state);
3979 4005
 	}

+ 1
- 1
js/markdown.min.js
Fichier diff supprimé car celui-ci est trop grand
Voir le fichier


+ 2
- 2
markdownjs.html Voir le fichier

@@ -440,12 +440,12 @@ The HTML on the WWW is often full of JS and CSS.
440 440
 *[JS]: JavaScript
441 441
 *[CSS]: Cascading Style Sheets
442 442
 
443
-Click [here](#top) to return to the top. Referenced link to [Google][google title="I prefer Duck Duck Go"].
443
+Click [here](#top) to return to the top. Referenced link to [Google][google].
444 444
 
445 445
 ![alt text](https://picsum.photos/100/75) ![alt text](https://picsum.photos/101/75 "With a title") ![][testimage]
446 446
 
447 447
 [testimage]: https://picsum.photos/102/75
448
-[google]: https://google.com
448
+[google]: https://google.com  "I prefer Duck Duck Go"
449 449
 
450 450
 Some verbatim &lt;span style="color:green;"&gt;HTML&lt;/span&gt;. A script tag will be ignored. &lt;script&gt;&lt;/script&gt;
451 451
 			</textarea>

+ 4
- 5
testjs.html Voir le fichier

@@ -1178,12 +1178,11 @@
1178 1178
 				}
1179 1179
 
1180 1180
 				test_footnotes() {
1181
-					let markdown = 'Lorem ipsum[^1] dolor[^2] sit[^1] amet\n\n[^1]: A footnote\n[^2]: Another footnote';
1182
-					let expected = '<p>Lorem ipsum<sup id="footnoteref_1"><a href="#footnote_1">1</a></sup> ' +
1183
-						'dolor<sup id="footnoteref_2"><a href="#footnote_2">2</a></sup> ' +
1184
-						'sit<sup id="footnoteref_3"><a href="#footnote_1">1</a></sup> amet</p> ' +
1181
+					let markdown = 'Lorem ipsum[^1] dolor[^abc] sit[^1] amet\n\n[^1]: A footnote\n[^abc]: Another footnote';
1182
+					let expected = '<p>Lorem ipsum<sup class="footnote" id="footnoteref_1"><a href="#footnote_1">1</a></sup> ' +
1183
+						'dolor<sup class="footnote" id="footnoteref_2"><a href="#footnote_2">2</a></sup> ' +
1184
+						'sit<sup class="footnote" id="footnoteref_3"><a href="#footnote_1">1</a></sup> amet</p> ' +
1185 1185
 						'<div class="footnotes">' +
1186
-						'<hr/>' +
1187 1186
 						'<ol>' +
1188 1187
 						'<li value="1" id="footnote_1">A footnote <a href="#footnoteref_1" class="footnote-backref">↩︎</a> <a href="#footnoteref_3" class="footnote-backref">↩︎</a></li> ' +
1189 1188
 						'<li value="2" id="footnote_2">Another footnote <a href="#footnoteref_2" class="footnote-backref">↩︎</a></li> ' +

Chargement…
Annuler
Enregistrer