Html2Amp is a simple converter from HTML code to "AMP Html".
Build | Nightly Build | Release | |
---|---|---|---|
Master | n / a | n / a | |
Development | n / a |
- Install "HtmlToAmp" package using the "Package Manager Console" in Visual Studio.
Install-Package HtmlToAmp
- Download the package from nuget.org.
var converter = new HtmlToAmpConverter();
string ampHtml = converter.ConvertFromHtml("<h1>Getting started</h1>");
Using WithConfiguration(RunConfiguration)
method you can configure the converter with specified settings in RunConfiguration
object. The following example shows how to configure the converter to resolve relative URLs to your website:
var htmlToAmpConverter = new HtmlToAmpConverter();
htmlToAmpConverter.WithConfiguration(
new RunConfiguration
{
RelativeUrlsHost = "http://mywebsite.com"
});
Html2Amp is built using "HTML sanitizers" which are the main extensibility point. You can add your custom sanitizers or extend the existing ones. Using WithSanitizers(HashSet<ISanitizer>)
method you can configure the collection of sanitizers which will be used for the conversion:
var htmlToAmpConverter = new HtmlToAmpConverter();
htmlToAmpConverter.WithSanitizers(
new HashSet<ISanitizer>
{
new ImageSanitizer()
});
The library is designed to be user friendly, that's why it supports fluent configuration:
new HtmlToAmpConverter().WithSanitizers(...).WithConfiguration(...)
- Implement handling of relative URLs in ImageSanitizer
- Href should not start with javascript: - so it should be removed.Read more about on: [https://www.ampproject.org/docs/reference/spec.html#ancr](Link URL)
- Remove all attributes starting with prefix "on" - Attribute names starting with on (such as onclick or onmouseover) are disallowed in AMP HTML. The attribute with the literal name on (no suffix) is allowed.
- Remove xml attributes - XML-related attributes, such as xmlns, xml:lang, xml:base, and xml:space are disallowed in AMP HTML.
- Implement iframes sanitizer
- Implement media sanitizers
- amp-youtube
- amp-audio
- Implement Socials media sanitizers
- amp-facebook
- amp-twitter
- amp-social-share
- Implement Html helper for asp.net mvc framework
- Exporting all third party dependencies used after conversion. Also Html helper should be implemented to add dependencies into "scripts" section. Examples: <script async custom-element="amp-facebook" src="https://cdn.ampproject.org/v0/amp-facebook-0.1.js"></script> <script async custom-element="amp-youtube" src="https://cdn.ampproject.org/v0/amp-youtube-0.1.js"></script>
- Implement media sanitizers
- amp-soundcloud
- amp-dailymotion
- amp-vine
- amp-jwplayer
- amp-kaltura-player
- amp-vimeo
- Implement Socials media sanitizers
- amp-instagram
- amp-pinterest
- Minifiy "AMP HTML"
Todo
Todo
[https://www.ampproject.org/docs/get_started/technical_overview.html](Link URL)
[https://www.ampproject.org/docs/reference/spec.html#ancr](Link URL)
[http://searchengineland.com/get-started-accelerated-mobile-pages-amp-240688](Link URL)
[https://www.ampproject.org/docs/reference/extended.html](Link URL)
[https://github.com/ampproject/amphtml/blob/master/spec/amp-html-format.md](Link URL)
[https://www.smashingmagazine.com/2016/02/everything-about-google-accelerated-mobile-pages](Link URL)