lonekorean / wordpress-export-to-markdown

Converts a WordPress export XML file into Markdown files.
MIT License
1.07k stars 216 forks source link

Issues with date and created #103

Closed bksubhuti closed 6 months ago

bksubhuti commented 6 months ago

I had issues with date versus created is a very annoying bug.
The video I watched said it was a problem and I too also had the problem. Created does not work for sorting. So I had to write a script to change that.

import 'dart:io';

void main(List<String> arguments) {
  if (arguments.isEmpty) {
    print('Usage: dart add_date.dart <directory>');
    exit(1);
  }

  final directoryPath = arguments[0];
  final directory = Directory(directoryPath);

  if (!directory.existsSync()) {
    print('Error: Directory not found.');
    exit(1);
  }

  directory
      .listSync(recursive: true)
      .whereType<File>()
      .where((file) => file.path.endsWith('.md'))
      .forEach((file) {
    final lines = file.readAsLinesSync();
    final updatedLines = updateDateInLines(lines);

    if (updatedLines != null) {
      file.writeAsStringSync(updatedLines.join('\n'));
      print('${file.path} updated.');
    }
  });
}

List<String>? updateDateInLines(List<String> lines) {
  bool modified = false;
  int dateIndex = lines.indexWhere(
      (line) => line.startsWith('date:') || line.startsWith('created:'));

  if (dateIndex != -1) {
    final currentLine = lines[dateIndex];
    // Check if it starts with 'created:', if so, convert it.
    if (currentLine.startsWith('created:')) {
      final dateValue = currentLine.replaceFirst('created:', 'date:').trim();
      lines[dateIndex] = dateValue;
      modified = true;
    }
  } else {
    // If no 'date:' or 'created:' tag is found, add a default 'date:' tag at the beginning.
    lines.insert(0, 'date: 2012-01-01T00:00:00+00:00');
    modified = true;
  }

  return modified ? lines : null;
}
lonekorean commented 6 months ago

Following up on our discussion here: https://github.com/lonekorean/wordpress-export-to-markdown/pull/67#issuecomment-1951786121

In the video, he's using Flowershow's fork of my repo, which I'm guessing is what you also used.

My repo uses date (here), while Flowershow's repo changed it to created (here). Neither one is right or wrong, it's just what was picked. I imagine Flowershow changed it to created to fit their own process, while the guy in the video wanted it to be date.

If you (or anyone else coming across this issue) need to change what the date field is named, you can edit it in the line of code I linked above (either in my repo or Flowershow's, whichever you want to use) before running the script. I have an idea to allow easier customization of field names, but that'll come later.

Hope that helps!

bksubhuti commented 6 months ago

oh.. okay.. I'll leave a comment on the video.. not sure how I got to the original.. but that is good that it can be corrected. I also had a huge problem.. I'll make a separate issue. You can close if the original issue another person posted is fixed.

lonekorean commented 6 months ago

sounds good, closing this one out 👍