html 到 markdown

在線上將 HTML 轉換為 Markdown

HTML 格式是網站的主要格式之一,因為大多數瀏覽器都支援此標準。而 Markdown 文件是使用 Markdown 語言的幾種可能方言之一創建的文本文件。它使用純文字格式,但包含指定如何設定文字格式的內聯文本符號(例如,\bold\ 表示粗體文本,或其他用於斜體、縮排、標題等的標記)。請注意,Markdown 檔案可能使用 .MD、.MARKDN 和 .MDOWN 副檔名,其中「.markdown」和「.md」副檔名是最受歡迎的。因此,在本文中,我們將討論使用 REST API 將 HTML 轉換為 Markdown 的詳細資訊。

HTML 到 Markdown 轉換 API

Aspose.HTML Cloud 是領先的 HTML 檔案處理 API,提供操作 HTML 檔案的功能。它還支援將 HTML 轉換為 PDFJPGTIFFXPS 和其他支援的格式 的功能。它還支援將 HTML 格式轉換為 MD 格式的功能。現在為了使用Java語言開發HTML到Markdown轉換器,我們首先需要在maven建置類型的pom.xml檔案中加入Aspose.HTML Cloud SDK for Java參考。

<repositories> 
    <repository>
        <id>aspose-cloud</id>
        <name>artifact.aspose-cloud-releases</name>
        <url>http://artifact.aspose.cloud/repo</url>
    </repository>   
</repositories>

<dependencies>
    <dependency>
        <groupId>com.aspose</groupId>
        <artifactId>aspose-words-cloud</artifactId>
        <version>22.8.0</version>
    </dependency>
</dependencies>

安裝後,請使用 GitHub 或 Google 帳戶透過 Aspose.Cloud 儀表板 在我們的雲端服務上建立免費訂閱帳戶。或者,只需建立新帳戶 並獲取您的客戶憑證詳細資訊。

在 Java 中將 HTML 轉換為 Markdown

請依照下面指定的說明將 HTML 轉換為 Markdown。

  • 首先,根據 Configuration.setAPPSID 和 Configuration.setAPIKEY 方法傳遞客戶端憑證。
  • 其次,設定 setBasePath(..)、setAuthPath(..) 的詳細信息,並將 setUserAgent(…) 指定為 WebKit。
  • 第三,將 true 傳遞給 setDebug(..) 方法。
  • 配置完成後,請建立ConversionApi類別的物件。
  • 最後,呼叫 PutConvertDocumentToMarkdown(…) 方法將 HTML 轉換為 Markdown 並將輸出保存在雲端儲存中。
// 有關更多範例,請造訪 https://github.com/aspose-html-cloud/aspose-html-cloud-java

try
    {
    // 從 https://dashboard.aspose.cloud/ 取得 ClientID 和 ClientSecret
    String clientId = "bbf94a2c-6d7e-4020-b4d2-b9809741374e";
    String clientSecret = "1c9379bb7d701c26cc87e741a29987bb";
  
    // Api呼叫詳情
    com.aspose.html.Configuration.setAPP_SID(clientId);
    com.aspose.html.Configuration.setAPI_KEY(clientSecret);
    com.aspose.html.Configuration.setBasePath("https://api.aspose.cloud/v3.0");
    com.aspose.html.Configuration.setAuthPath("https://api.aspose.cloud/connect/token");
    com.aspose.html.Configuration.setUserAgent("WebKit");
    com.aspose.html.Configuration.setDebug(true);
        
    // 建立Aspose.HTML Cloud API的對象
    com.aspose.html.api.ConversionApi htmlApi = new ApiClient().createService(ConversionApi.class);
     	
    // 來自雲端儲存的html文檔
    String name = "list.html";
    	
    String folder = null; // The folder in the storage. Should exist.
    String storage = "Internal"; // Name of the storage. null
    	
    // 準備調用執行
    retrofit2.Call<okhttp3.ResponseBody> call = htmlApi.PutConvertDocumentToMarkdown(name, "Converted.md", true, folder, storage);
      
    System.out.println("HTML to Markdown conversion sucessfull !");
    }catch(Exception ex)
    {
        System.out.println(ex);
    }

使用 cURL 指令將 HTML 轉換為 MD

由於Aspose.HTML Cloud是基於REST架構,因此可以透過cURL命令輕鬆存取它。現在,作為先決條件,我們需要根據您在雲端儀表板上的帳戶指定的用戶端憑證產生 JWT 存取權杖。現在,請執行以下命令來產生 JWT 令牌:

curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=bbf94a2c-6d7e-4020-b4d2-b9809741374e&client_secret=1c9379bb7d701c26cc87e741a29987bb" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"

一旦我們有了 JWT 令牌,請執行以下命令將 HTML 轉換為 MD。

curl -v -X PUT "https://api.aspose.cloud/html/list.html/convert/md?outPath=Converted.md&useGit=false" \
-H  "accept: application/json" \
-H  "authorization: Bearer <JWT Token>"

結論

本文為學習使用 Java Cloud SDK 開發 HTML 到 Markdown 轉換器的步驟提供了很好的資源。我們發現,透過更少的程式碼行,我們成功地將 HTML 轉換為 MD 格式。您也可以考慮在 Web 瀏覽器中透過 Swagger UI 來探索 API。如果您有任何相關疑問,或遇到任何問題,請隨時透過免費的技術支援論壇與我們聯繫。

相關文章

我們強烈建議您閱讀以下文章以了解有關它們的更多資訊: