Skip to content

Commit 3cf910b

Browse files
authored
Migrate example to pkg:web, update minimum required Dart version (dart-archive/markdown#582)
1 parent 6450bbf commit 3cf910b

File tree

6 files changed

+46
-34
lines changed

6 files changed

+46
-34
lines changed

pkgs/markdown/.github/workflows/test-package.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ jobs:
4747
matrix:
4848
# Add macos-latest and/or windows-latest if relevant for this package.
4949
os: [ubuntu-latest]
50-
sdk: [3.1, dev]
50+
sdk: [3.2, dev]
5151
steps:
5252
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11
5353
- uses: dart-lang/setup-dart@fedb1266e91cf51be2fdb382869461a434b920a3

pkgs/markdown/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 7.2.2-wip
2+
3+
* Require Dart `^3.2.0`.
4+
15
## 7.2.1
26

37
* Address a termination issue with GitHub alert syntax parsing.

pkgs/markdown/example/app.dart

+32-26
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,18 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'dart:async';
6-
import 'dart:html';
6+
import 'dart:js_interop';
77

88
import 'package:markdown/markdown.dart' as md;
9+
import 'package:web/web.dart';
910

1011
import 'highlight.dart';
1112

12-
final markdownInput = querySelector('#markdown') as TextAreaElement;
13-
final htmlDiv = querySelector('#html') as DivElement;
14-
final versionSpan = querySelector('.version') as SpanElement;
13+
final markdownInput =
14+
document.querySelector('#markdown') as HTMLTextAreaElement;
15+
final htmlDiv = document.querySelector('#html') as HTMLDivElement;
16+
final versionSpan = document.querySelector('.version') as HTMLSpanElement;
1517

16-
final nullSanitizer = NullTreeSanitizer();
1718
const typing = Duration(milliseconds: 150);
1819
const introText = '''Markdown is the **best**!
1920
@@ -26,9 +27,10 @@ const introText = '''Markdown is the **best**!
2627
* ...and _so much more_...''';
2728

2829
// Flavor support.
29-
final basicRadio = querySelector('#basic-radio') as HtmlElement;
30-
final commonmarkRadio = querySelector('#commonmark-radio') as HtmlElement;
31-
final gfmRadio = querySelector('#gfm-radio') as HtmlElement;
30+
final basicRadio = document.querySelector('#basic-radio') as HTMLElement;
31+
final commonmarkRadio =
32+
document.querySelector('#commonmark-radio') as HTMLElement;
33+
final gfmRadio = document.querySelector('#gfm-radio') as HTMLElement;
3234
md.ExtensionSet? extensionSet;
3335

3436
final extensionSets = {
@@ -54,7 +56,7 @@ void main() {
5456
}
5557

5658
// GitHub is the default extension set.
57-
gfmRadio.attributes['checked'] = '';
59+
gfmRadio.attributes.getNamedItem('checked')?.value = '';
5860
gfmRadio.querySelector('.glyph')!.text = 'radio_button_checked';
5961
extensionSet = extensionSets[gfmRadio.id];
6062
_renderMarkdown();
@@ -65,19 +67,16 @@ void main() {
6567
}
6668

6769
void _renderMarkdown([Event? event]) {
68-
final markdown = markdownInput.value!;
70+
final markdown = markdownInput.value;
6971

70-
htmlDiv.setInnerHtml(
71-
md.markdownToHtml(markdown, extensionSet: extensionSet),
72-
treeSanitizer: nullSanitizer,
73-
);
72+
htmlDiv.innerHTML = md.markdownToHtml(markdown, extensionSet: extensionSet);
7473

75-
for (final block in htmlDiv.querySelectorAll('pre code')) {
74+
for (final block in htmlDiv.querySelectorAll('pre code').items) {
7675
try {
7776
highlightElement(block);
7877
} catch (e) {
79-
window.console.error('Error highlighting markdown:');
80-
window.console.error(e);
78+
console.error('Error highlighting markdown:'.toJS);
79+
console.error(e.toString().toJS);
8180
}
8281
}
8382

@@ -107,29 +106,36 @@ void _typeItOut(String msg, int pos) {
107106
}
108107

109108
void _switchFlavor(Event e) {
110-
final target = e.currentTarget as HtmlElement;
111-
if (!target.attributes.containsKey('checked')) {
109+
final target = e.currentTarget as HTMLElement;
110+
if (target.attributes.getNamedItem('checked') == null) {
112111
if (basicRadio != target) {
113-
basicRadio.attributes.remove('checked');
112+
basicRadio.attributes.safeRemove('checked');
114113
basicRadio.querySelector('.glyph')!.text = 'radio_button_unchecked';
115114
}
116115
if (commonmarkRadio != target) {
117-
commonmarkRadio.attributes.remove('checked');
116+
commonmarkRadio.attributes.safeRemove('checked');
118117
commonmarkRadio.querySelector('.glyph')!.text = 'radio_button_unchecked';
119118
}
120119
if (gfmRadio != target) {
121-
gfmRadio.attributes.remove('checked');
120+
gfmRadio.attributes.safeRemove('checked');
122121
gfmRadio.querySelector('.glyph')!.text = 'radio_button_unchecked';
123122
}
124123

125-
target.attributes['checked'] = '';
124+
target.attributes.getNamedItem('checked')?.value = '';
126125
target.querySelector('.glyph')!.text = 'radio_button_checked';
127126
extensionSet = extensionSets[target.id];
128127
_renderMarkdown();
129128
}
130129
}
131130

132-
class NullTreeSanitizer implements NodeTreeSanitizer {
133-
@override
134-
void sanitizeTree(Node node) {}
131+
extension on NodeList {
132+
List<Node> get items => [
133+
for (var i = 0; i < length; i++) item(i)!,
134+
];
135+
}
136+
137+
extension on NamedNodeMap {
138+
void safeRemove(String qualifiedName) {
139+
if (getNamedItem(qualifiedName) != null) removeNamedItem(qualifiedName);
140+
}
135141
}

pkgs/markdown/example/highlight.dart

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
@JS('hljs')
6-
library hljs;
6+
library;
77

8-
import 'package:js/js.dart';
8+
import 'dart:js_interop';
9+
10+
import 'package:web/web.dart';
911

1012
@JS()
11-
external void highlightElement(Object block);
13+
external void highlightElement(Node block);

pkgs/markdown/lib/src/version.dart

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkgs/markdown/pubspec.yaml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: markdown
2-
version: 7.2.1
2+
version: 7.2.2-wip
33

44
description: >-
55
A portable Markdown library written in Dart that can parse Markdown into HTML.
@@ -11,7 +11,7 @@ executables:
1111
markdown:
1212

1313
environment:
14-
sdk: ^3.1.0
14+
sdk: ^3.2.0
1515

1616
dependencies:
1717
args: ^2.0.0
@@ -26,9 +26,9 @@ dev_dependencies:
2626
html: ^0.15.0
2727
http: ^1.0.0
2828
io: ^1.0.0
29-
js: ^0.7.0
3029
path: ^1.8.0
3130
pool: ^1.5.1
3231
tar: ^1.0.3
3332
test: ^1.16.0
33+
web: '>=0.4.2 <0.6.0'
3434
yaml: ^3.0.0

0 commit comments

Comments
 (0)