Skip to content

Commit

Permalink
content: Implement h1-h5
Browse files Browse the repository at this point in the history
Fixes: zulip#192
  • Loading branch information
sirpengi committed Jan 25, 2024
1 parent 1b0061e commit 38c40ad
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 12 deletions.
3 changes: 1 addition & 2 deletions lib/model/content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -927,8 +927,7 @@ class _ZulipContentParser {
case 'h5': headingLevel = HeadingLevel.h5; break;
case 'h6': headingLevel = HeadingLevel.h6; break;
}
if (headingLevel == HeadingLevel.h6 && classes.isEmpty) {
// TODO(#192) handle h1, h2, h3, h4, h5
if (headingLevel != null && classes.isEmpty) {
final parsed = parseBlockInline(element.nodes);
return HeadingNode(debugHtmlNode: debugHtmlNode,
level: headingLevel!,
Expand Down
15 changes: 12 additions & 3 deletions lib/widgets/content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,21 @@ class Heading extends StatelessWidget {

@override
Widget build(BuildContext context) {
// TODO(#192) h1, h2, h3, h4, h5 -- same as h6 except font size
assert(node.level == HeadingLevel.h6);
final double emHeight = switch(node.level) {
HeadingLevel.h1 => 1.4,
HeadingLevel.h2 => 1.3,
HeadingLevel.h3 => 1.2,
HeadingLevel.h4 => 1.1,
HeadingLevel.h5 => 1.05,
HeadingLevel.h6 => 1,
};
return Padding(
padding: const EdgeInsets.only(top: 15, bottom: 5),
child: _buildBlockInlineContainer(
style: const TextStyle(fontWeight: FontWeight.w600, height: 1.4),
style: TextStyle(
fontSize: kBaseFontSize * emHeight,
fontWeight: FontWeight.w600,
height: 1.4),
node: node));
}
}
Expand Down
14 changes: 7 additions & 7 deletions test/model/content_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,14 @@ void main() {
ParagraphNode(links: null, nodes: [TextNode('text')]),
]);

testParse('h1, h2, h3, h4, h5 unimplemented',
testParse('h1, h2, h3, h4, h5',
// "# one\n## two\n### three\n#### four\n##### five"
'<h1>one</h1>\n<h2>two</h2>\n<h3>three</h3>\n<h4>four</h4>\n<h5>five</h5>', [
blockUnimplemented('<h1>one</h1>'),
blockUnimplemented('<h2>two</h2>'),
blockUnimplemented('<h3>three</h3>'),
blockUnimplemented('<h4>four</h4>'),
blockUnimplemented('<h5>five</h5>'),
'<h1>one</h1>\n<h2>two</h2>\n<h3>three</h3>\n<h4>four</h4>\n<h5>five</h5>', const [
HeadingNode(level: HeadingLevel.h1, links: null, nodes: [TextNode('one')]),
HeadingNode(level: HeadingLevel.h2, links: null, nodes: [TextNode('two')]),
HeadingNode(level: HeadingLevel.h3, links: null, nodes: [TextNode('three')]),
HeadingNode(level: HeadingLevel.h4, links: null, nodes: [TextNode('four')]),
HeadingNode(level: HeadingLevel.h5, links: null, nodes: [TextNode('five')]),
]);
});

Expand Down
24 changes: 24 additions & 0 deletions test/widgets/content_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,30 @@ import 'page_checks.dart';
void main() {
TestZulipBinding.ensureInitialized();

group('Headings', () {
Future<void> prepareContent(WidgetTester tester, String html) async {
await tester.pumpWidget(MaterialApp(home: BlockContentList(nodes: parseContent(html).nodes)));
}

testWidgets('plain h6', (tester) async {
await prepareContent(tester,
// "###### six"
'<h6>six</h6>');
tester.widget(find.text('six'));
});

testWidgets('h1, h2, h3, h4, h5', (tester) async {
await prepareContent(tester,
// "# one\n## two\n### three\n#### four\n##### five"
'<h1>one</h1>\n<h2>two</h2>\n<h3>three</h3>\n<h4>four</h4>\n<h5>five</h5>');
tester.widget(find.text('one'));
tester.widget(find.text('two'));
tester.widget(find.text('three'));
tester.widget(find.text('four'));
tester.widget(find.text('five'));
});
});

group("CodeBlock", () {
Future<void> prepareContent(WidgetTester tester, String html) async {
await tester.pumpWidget(MaterialApp(home: BlockContentList(nodes: parseContent(html).nodes)));
Expand Down

0 comments on commit 38c40ad

Please sign in to comment.