processMarkdown function

Future<String> processMarkdown(
  1. String markdownDoc,
  2. String title,
  3. String partialsPath,
  4. String templatesPath,
)

Implementation

Future<String> processMarkdown(final String markdownDoc, final String title,
    final String partialsPath, final String templatesPath) async {
  final mdTitlePattern = RegExp("^# (.*)");
  Map frontMatter = {};
  String markdownBody = "";

  // Regular expression to match the YAML front matter
  final regex = RegExp(r'^---\n([\s\S]*?)\n---\n', multiLine: true);

  final frontmatterMatch = regex.firstMatch(markdownDoc);
  if (frontmatterMatch != null) {
    // Extract YAML front matter
    final yamlFrontMatter = frontmatterMatch.group(1);
    if (yamlFrontMatter == null) {
      throw Exception('No YAML front matter found.');
    }
    // Extract the remaining markdown content
    markdownBody = markdownDoc.replaceFirst(regex, '');
    print('YAML Front Matter:\n$yamlFrontMatter');
    // print('\nMarkdown Content:\n$markdownWithoutFrontMatter');
    frontMatter = y.loadYaml(yamlFrontMatter);
  } else {
    throw Exception('No YAML front matter found.');
  }

  Map docVariables = {};
  docVariables["header"] = "";
  docVariables["title"] = title;
  final match = mdTitlePattern.firstMatch(markdownBody);
  if (match != null) {
    docVariables["title"] = match[1]!;
  } else if (frontMatter["title"] != null) {
    docVariables["title"] = frontMatter["title"];
  } else {
    docVariables["header"] = "<h1>$title</h1>\n";
  }
  print("finished processing:$title");

  docVariables['body'] = m.markdownToHtml(
    markdownBody,
    inlineSyntaxes: [
      InlineHtmlSyntax(),
    ],
    blockSyntaxes: [
      TableSyntax(),
      FencedCodeBlockSyntax(),
      HeaderWithIdSyntax(),
      HorizontalRuleSyntax(),
    ],
  );

  Template? partialsFileResolver(String name) {
    final partial = File(p.join(partialsPath, name)).readAsStringSync();
    return Template(partial);
  }

  // find template to use for this page
  String templateName = frontMatter['template'] ?? '';

  print("CWD:${Directory.current.path}");

  String templateText =
      File('$templatesPath/$templateName.html').readAsStringSync();

  final template = Template(
    templateText,
    name: title,
    htmlEscapeValues: false,
    partialResolver: partialsFileResolver,
  );

  var rendered = template.renderString(docVariables);

  return rendered;
}