-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ref(shokunin): code updates for keywords extract
- Loading branch information
1 parent
0773989
commit 8f8235f
Showing
3 changed files
with
94 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright © 2023 Shokunin Static Site Generator. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
||
use std::collections::HashMap; | ||
|
||
/// Extracts keywords from the metadata and returns them as a vector of strings. | ||
/// | ||
/// This function takes a reference to a HashMap containing metadata key-value pairs. | ||
/// It looks for the "keywords" key in the metadata and extracts the keywords from its value. | ||
/// Keywords are expected to be comma-separated. The extracted keywords are trimmed of any | ||
/// leading or trailing whitespace, and returned as a vector of strings. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `metadata` - A reference to a HashMap containing metadata. | ||
/// | ||
/// # Returns | ||
/// | ||
/// A vector of strings representing the extracted keywords. | ||
/// | ||
pub fn extract_keywords(metadata: &HashMap<String, String>) -> Vec<String> { | ||
// Check if the "keywords" key exists in the metadata. | ||
// If it exists, split the keywords using a comma and process each keyword. | ||
// If it doesn't exist, return an empty vector as the default value. | ||
metadata | ||
.get("keywords") | ||
.map(|keywords| { | ||
// Split the keywords using commas and process each keyword. | ||
keywords | ||
.split(',') | ||
.map(|kw| kw.trim().to_string()) // Trim whitespace from each keyword. | ||
.collect::<Vec<_>>() // Collect the processed keywords into a vector. | ||
}) | ||
.unwrap_or_default() // Return an empty vector if "keywords" is not found. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters