Skip to content

Commit 0ec82ce

Browse files
committed
content: Implement h1-h5
Fixes: #192
1 parent 3c91597 commit 0ec82ce

File tree

4 files changed

+42
-13
lines changed

4 files changed

+42
-13
lines changed

lib/model/content.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -927,11 +927,10 @@ class _ZulipContentParser {
927927
case 'h5': headingLevel = HeadingLevel.h5; break;
928928
case 'h6': headingLevel = HeadingLevel.h6; break;
929929
}
930-
if (headingLevel == HeadingLevel.h6 && classes.isEmpty) {
931-
// TODO(#192) handle h1, h2, h3, h4, h5
930+
if (headingLevel != null && classes.isEmpty) {
932931
final parsed = parseBlockInline(element.nodes);
933932
return HeadingNode(debugHtmlNode: debugHtmlNode,
934-
level: headingLevel!,
933+
level: headingLevel,
935934
links: parsed.links,
936935
nodes: parsed.nodes);
937936
}

lib/widgets/content.dart

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,22 @@ class Heading extends StatelessWidget {
136136

137137
@override
138138
Widget build(BuildContext context) {
139-
// TODO(#192) h1, h2, h3, h4, h5 -- same as h6 except font size
140-
assert(node.level == HeadingLevel.h6);
139+
// Em-heights taken from zulip:web/styles/rendered_markdown.css .
140+
final emHeight = switch(node.level) {
141+
HeadingLevel.h1 => 1.4,
142+
HeadingLevel.h2 => 1.3,
143+
HeadingLevel.h3 => 1.2,
144+
HeadingLevel.h4 => 1.1,
145+
HeadingLevel.h5 => 1.05,
146+
HeadingLevel.h6 => 1.0,
147+
};
141148
return Padding(
142149
padding: const EdgeInsets.only(top: 15, bottom: 5),
143150
child: _buildBlockInlineContainer(
144-
style: const TextStyle(fontWeight: FontWeight.w600, height: 1.4),
151+
style: TextStyle(
152+
fontSize: kBaseFontSize * emHeight,
153+
fontWeight: FontWeight.w600,
154+
height: 1.4),
145155
node: node));
146156
}
147157
}

test/model/content_test.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -219,14 +219,14 @@ void main() {
219219
ParagraphNode(links: null, nodes: [TextNode('text')]),
220220
]);
221221

222-
testParse('h1, h2, h3, h4, h5 unimplemented',
222+
testParse('h1, h2, h3, h4, h5',
223223
// "# one\n## two\n### three\n#### four\n##### five"
224-
'<h1>one</h1>\n<h2>two</h2>\n<h3>three</h3>\n<h4>four</h4>\n<h5>five</h5>', [
225-
blockUnimplemented('<h1>one</h1>'),
226-
blockUnimplemented('<h2>two</h2>'),
227-
blockUnimplemented('<h3>three</h3>'),
228-
blockUnimplemented('<h4>four</h4>'),
229-
blockUnimplemented('<h5>five</h5>'),
224+
'<h1>one</h1>\n<h2>two</h2>\n<h3>three</h3>\n<h4>four</h4>\n<h5>five</h5>', const [
225+
HeadingNode(level: HeadingLevel.h1, links: null, nodes: [TextNode('one')]),
226+
HeadingNode(level: HeadingLevel.h2, links: null, nodes: [TextNode('two')]),
227+
HeadingNode(level: HeadingLevel.h3, links: null, nodes: [TextNode('three')]),
228+
HeadingNode(level: HeadingLevel.h4, links: null, nodes: [TextNode('four')]),
229+
HeadingNode(level: HeadingLevel.h5, links: null, nodes: [TextNode('five')]),
230230
]);
231231
});
232232

test/widgets/content_test.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,26 @@ import 'page_checks.dart';
2424
void main() {
2525
TestZulipBinding.ensureInitialized();
2626

27+
group('Headings', () {
28+
Future<void> prepareContent(WidgetTester tester, String html) async {
29+
await tester.pumpWidget(MaterialApp(home: BlockContentList(nodes: parseContent(html).nodes)));
30+
}
31+
32+
testWidgets('plain h6', (tester) async {
33+
await prepareContent(tester,
34+
// "###### six"
35+
'<h6>six</h6>');
36+
tester.widget(find.text('six'));
37+
});
38+
39+
testWidgets('smoke test for h1, h2, h3, h4, h5', (tester) async {
40+
await prepareContent(tester,
41+
// "# one\n## two\n### three\n#### four\n##### five"
42+
'<h1>one</h1>\n<h2>two</h2>\n<h3>three</h3>\n<h4>four</h4>\n<h5>five</h5>');
43+
check(find.byType(Heading).evaluate()).length.equals(5);
44+
});
45+
});
46+
2747
group("CodeBlock", () {
2848
Future<void> prepareContent(WidgetTester tester, String html) async {
2949
await tester.pumpWidget(MaterialApp(home: BlockContentList(nodes: parseContent(html).nodes)));

0 commit comments

Comments
 (0)