WordpressToMarkdown 1.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package WordpressToMarkdown --version 1.1.0                
NuGet\Install-Package WordpressToMarkdown -Version 1.1.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="WordpressToMarkdown" Version="1.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add WordpressToMarkdown --version 1.1.0                
#r "nuget: WordpressToMarkdown, 1.1.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install WordpressToMarkdown as a Cake Addin
#addin nuget:?package=WordpressToMarkdown&version=1.1.0

// Install WordpressToMarkdown as a Cake Tool
#tool nuget:?package=WordpressToMarkdown&version=1.1.0                

WordpressToMarkdown

NuGet Downloads

Français
English


C'est une librairie qui permet de convertir un flux JSON d'un site wordpress en MarkDown.
Un live sur wp-to-markdown.ctrl-alt-suppr.dev

Utilisations

Soit il faut donner une URL d'un site (voir ici pour les infos) à la class WordpressCollector qui va récupèrer le flux JSON et le convertir en markdown, comme par exemple :

// Récupère les 5 derniers articles
string urlWordpress = "https://www.ctrl-alt-suppr.dev/wp-json/wp/v2/posts?per_page=5";

WordpressCollector converterWordpress = new WordpressCollector();
List<MarkdownResult> resultConvertMd = await converter.GetPosts(urlWordpress);

ou alors passer directement le contenu à la class ConverterWordpress :

ConverterWordpress convert = new ConverterWordpress();
string markdown = await convert.ConvertToMarkdownAsync("Titre de l'article", contenuBrut);

Il est possible de fournir ces propres transformations. Il faut implémenter l'interface ITransformWordpress :

namespace WordpressToMarkdown
{
	public interface ITransformWordpress
	{
		/// <summary>
		/// Récupère le contenu en string, effectu les transformations
		/// et retourne le contenu.
		/// </summary>
		/// <param name="content"></param>
		/// <returns></returns>
		string Transform(string content);
	}
}

Ensuite, reste plus qu'à fournir vos transformations à la class ConverterWordpress sur la méthode :

public Task<string> ConvertToMarkdownAsync(string title, string contentPost, params ITransformWordpress[] transformations);

Conversion

Pour les blocks de code, j'utilise sur Wordpress l'extension Enlighter, du coup mes transformations sont basées sur certains "mots clés" de cette extension.
Par exemple, pour avoir le langage, je prend sur l'attribut : data-enlighter-language.

Problème connu

Pour la conversion, j'utilise la librairie HtmlAgilityPack, et il arrive qu'elle ajoute des "balises" fermantes sur des portions de code.
Comme par exemple :

namespace WebApiGraphQl.Data
{
    public interface IDataAccess
    {
	/// <summary>
	/// Retourne toutes personnes
	/// </summary>
	/// <returns></returns>
	IEnumerable<Personne> GetAll();

	/// <summary>
	/// Retourne la personne par rapport à son ID
	/// </summary>
	/// <param name="id" />
	/// <returns></returns>
	Personne GetPersonne(Guid id);
    }
}</Personne>

Elle a ajouté </Personne> car elle considère IEnumerable<Personne> comme une balise ouvrante et elle ferme automatiquement, c'est pour cela que ce n'est pas parfait, il peut y avoir quelques retouches.

URL

Wordpress JSON

Pour connaitre l'URL d'un site wordpress, il faut prendre cette base :
https://url-du-wordpress/wp-json/wp/v2/
Ensuite, pour récupérer plusieurs posts d'un coup :
https://url-du-wordpress/wp-json/wp/v2/posts?per_page=5
Et pour avoir un post en particulier, il faut :
http://url-du-wordpress/wp-json/wp/v2/posts?slug=TITRE-DU-POST


Use

Either you have to give a URL of a site (see here for info) à la class WordpressCollector to the WordpressCollector class which will retrieve the JSON stream and convert it into a markdown, such as :

// Get the 5 last posts
string urlWordpress = "https://www.ctrl-alt-suppr.dev/wp-json/wp/v2/posts?per_page=5";

WordpressCollector converterWordpress = new WordpressCollector();
List<MarkdownResult> resultConvertMd = await converter.GetPosts(urlWordpress);

or go directly to the ConverterWordpress class :

ConverterWordpress convert = new ConverterWordpress();
string markdown = await convert.ConvertToMarkdownAsync("Titre de l'article", contenuBrut);

It is possible to provide these own transformations. It is necessary to implement the interface ITransformWordpress:

namespace WordpressToMarkdown
{
	public interface ITransformWordpress
	{
		/// <summary>
		/// Récupère le contenu en string, effectu les transformations
		/// et retourne le contenu.
		/// </summary>
		/// <param name="content"></param>
		/// <returns></returns>
		string Transform(string content);
	}
}

Then, it remains more than to provide your transformations to the ConverterWordpress class on the method :

public Task<string> ConvertToMarkdownAsync(string title, string contentPost, params ITransformWordpress[] transformations);

URL-JSON

To know the URL of a wordpress site, you have to take this basis :
https://url-du-wordpress/wp-json/wp/v2/
Then, to retrieve several posts at once :
https://url-du-wordpress/wp-json/wp/v2/posts?per_page=5
And to have a particular post, you have to :
http://url-du-wordpress/wp-json/wp/v2/posts?slug=TITRE-DU-POST

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.4 462 5/6/2022
1.1.3 436 4/29/2022
1.1.2 421 4/22/2022
1.1.1 426 4/21/2022
1.1.0 425 3/20/2022
1.0.0 408 3/20/2022