extractTitle method
- String title
///////////////////////////////////////////////////////////////////// Extract the title from the title string. The actual title is expected to be the fist line and begins with the markdown #. We strip the #. If not title is found the return the empty string.
Implementation
// PDF Creation
/// Extract the title from the title string. The actual title is expected to
/// be the fist line and begins with the markdown #. We strip the #. If not
/// title is found the return the empty string.
String extractTitle(String title) {
List<String> lines = title.split('\n');
for (String line in lines) {
// Trim leading and trailing spaces and check if the string is not empty.
if (line.trim().isNotEmpty) {
// Use a regular expression to check if the string starts with spaces
// followed by a #.
RegExp regExp = RegExp(r'^\s*#(.*)');
// If the string matches the pattern, return the part after the # and
// spaces. Return an empty string if the first non-empty string doesn't
// start with #
Match? match = regExp.firstMatch(line);
return match != null ? match.group(1)?.trim() ?? '' : '';
}
}
return '';
}