Skip to content

Fix custom header id being overwritten#177

Open
longtn-imt wants to merge 1 commit intoFlutter-Bounty-Hunters:mainfrom
longtn-imt:patch-1
Open

Fix custom header id being overwritten#177
longtn-imt wants to merge 1 commit intoFlutter-Bounty-Hunters:mainfrom
longtn-imt:patch-1

Conversation

@longtn-imt
Copy link

@longtn-imt longtn-imt commented Jan 3, 2025

When my doc gen-id use utf-8 it errors.

Example code with bug

import 'package:markdown/markdown.dart';
import 'package:static_shock/static_shock.dart';

Future<void> main(List<String> arguments) async {
  // Configure the static website generator.
  final StaticShock staticShock = StaticShock()
    // Here, you can directly hook into the StaticShock pipeline. For example,
    // you can copy an "images" directory from the source set to build set:
    ..pick(DirectoryPicker.parse("images"))
    // All 3rd party behavior is added through plugins, even the behavior
    // shipped with Static Shock.
    ..plugin(MarkdownPlugin(
      renderOptions: MarkdownRenderOptions(
        blockSyntaxes: [
          MyHeaderWithIdSyntax(),
        ],
      ),
    ))
    ..plugin(const JinjaPlugin())
    ..plugin(const PrettyUrlsPlugin())
    ..plugin(const RedirectsPlugin())
    ..plugin(const SassPlugin())
    ..plugin(DraftingPlugin(
      showDrafts: arguments.contains("preview"),
    ));

  // Generate the static website.
  await staticShock.generateSite();
}

class MyHeaderWithIdSyntax extends HeaderWithIdSyntax {
  @override
  Node parse(BlockParser parser) {
    final element = super.parse(parser) as Element;

    element.generatedId = element.children?.firstOrNull?.textContent
        .toLowerCase()
        .trim()
        .replaceAll(RegExp(r'\s'), '-')
        .replaceAll(RegExp(r'[^\p{L}\p{N}\p{P}]', unicode: true), '');

    return element;
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant