Skip to content

Commit

Permalink
Update privacy policy anchor hash algorithm. (#1146)
Browse files Browse the repository at this point in the history
We now replace umlaute and diacritics.

| Input | Old | New |
|--------|--------|--------|
| 1. Einführung | 1-einfhrung | 1-einfuehrung |
| abc pokémon | abc-pokmon | abc-pokemon | 

Fixes #1145
  • Loading branch information
Jonas-Sander authored Nov 5, 2023
1 parent 2870d8f commit e961f6b
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 26 deletions.
26 changes: 24 additions & 2 deletions app/lib/privacy_policy/src/ui/sharezone_markdown_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
// SPDX-License-Identifier: EUPL-1.2

import 'package:markdown/markdown.dart';
import 'package:diacritic/diacritic.dart' as pkg;

/// We define our own classes for automatic ID generation as we don't want any
/// chance of breakage if `package:markdown` changes/updates its algorithm.
///
/// It's the exact same algorithm (for now).
///
/// It might break if we e.g. would use one updated algorithm in our backend to
/// generate the anchor ids for the table of contents section list, but our
/// client uses an old algorithm to generate/search for the anchor ids inside
Expand Down Expand Up @@ -86,5 +85,28 @@ String _generateAnchorHashFromElement(Element element) =>
String generateAnchorHash(String text) => text
.toLowerCase()
.trim()
.replaceAllMapped(regexp, (char) => _replacementMap[char.group(0)]!)
.removeDiacritics()
.replaceAll(RegExp(r'[^a-z0-9 _-]'), '')
.replaceAll(RegExp(r'\s'), '-');

// Match every character in [_replacementMap.keys].
RegExp get regexp => RegExp(_replacementMap.keys.join('|'));

const _replacementMap = {
'Ä': 'ae',
'ä': 'ae',
'Ö': 'oe',
'ö': 'oe',
'Ü': 'ue',
'ü': 'ue',
'ẞ': 'ss',
'ß': 'ss',
};

extension on String {
/// E.g. `é` -> `e`
String removeDiacritics() {
return pkg.removeDiacritics(this);
}
}
8 changes: 8 additions & 0 deletions app/pubspec.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ dependencies:
path: ../lib/date
design:
path: ../lib/design
diacritic: ^0.1.4
dio: ^5.3.2
dynamic_links:
path: ../lib/dynamic_links
Expand Down
89 changes: 65 additions & 24 deletions app/test/privacy_policy/generate_anchor_hash_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,70 @@ import 'package:sharezone/privacy_policy/src/privacy_policy_src.dart';
void main() {
/// We are testing this since changing the anchor hash generation algorithm
/// might break stuff. See comments in [generateAnchorHash] file.
test('Generates correct anchor hashes', () {
final sections = [
'1. Einführung',
'2. Kontaktinformationen',
'3. Wichtige Begriffe, die du kennen solltest',
'4. Welche Informationen erfassen wir grundsätzlich?',
'5. An wen geben wir deine Daten weiter?',
'6. Wie lange speichern wir deine Daten?',
'7. Welche Rechte hast du?',
'Glückwunsch, du hast es geschafft'
];

final anchorHashes = sections.map(generateAnchorHash).toList();

expect(anchorHashes, [
'1-einfhrung',
'2-kontaktinformationen',
'3-wichtige-begriffe-die-du-kennen-solltest',
'4-welche-informationen-erfassen-wir-grundstzlich',
'5-an-wen-geben-wir-deine-daten-weiter',
'6-wie-lange-speichern-wir-deine-daten',
'7-welche-rechte-hast-du',
'glckwunsch-du-hast-es-geschafft',
]);
group('Generates correct anchor hashes', () {
test('real life examples', () {
final sections = [
'1. Einführung',
'2. Kontaktinformationen',
'3. Wichtige Begriffe, die du kennen solltest',
'4. Welche Informationen erfassen wir grundsätzlich?',
'5. An wen geben wir deine Daten weiter?',
'6. Wie lange speichern wir deine Daten?',
'7. Welche Rechte hast du?',
'Glückwunsch, du hast es geschafft',
];

final anchorHashes = sections.map(generateAnchorHash).toList();

expect(anchorHashes, [
'1-einfuehrung',
'2-kontaktinformationen',
'3-wichtige-begriffe-die-du-kennen-solltest',
'4-welche-informationen-erfassen-wir-grundsaetzlich',
'5-an-wen-geben-wir-deine-daten-weiter',
'6-wie-lange-speichern-wir-deine-daten',
'7-welche-rechte-hast-du',
'glueckwunsch-du-hast-es-geschafft',
]);
});
test('replaces special chars', () {
final sections = [
r'1-?!=""§$pokémon~+',
'Ist das eine Frage???',
'Wort-mit-Bindestrichen'
];

final anchorHashes = sections.map(generateAnchorHash).toList();

expect(anchorHashes, [
'1-pokemon',
'ist-das-eine-frage',
'wort-mit-bindestrichen',
]);
});

test('replaces umlaute', () {
final umlaute = [
'ä',
'Ä',
'ö',
'Ö',
'ü',
'Ü',
'ß',
];

final anchorHashes = umlaute.map(generateAnchorHash).toList();

expect(anchorHashes, [
'ae',
'ae',
'oe',
'oe',
'ue',
'ue',
'ss',
]);
});
});
}

0 comments on commit e961f6b

Please sign in to comment.