Skip to content

Commit

Permalink
Merge pull request #19 from h-sugawara/release/v1.1.0
Browse files Browse the repository at this point in the history
Release/v1.1.0
  • Loading branch information
h-sugawara authored Dec 26, 2023
2 parents 9b458e4 + c2924a9 commit 8e955ce
Show file tree
Hide file tree
Showing 20 changed files with 407 additions and 287 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/lint-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ on:
push:
branches-ignore:
- main
- 'release/**'
tags-ignore:
- '**'
paths:
- '*.js'
- '**.js'

jobs:
lint_and_test:
Expand Down
49 changes: 15 additions & 34 deletions .github/workflows/publish-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ jobs:
new_release_name: ${{ steps.new_release.outputs.name }}
new_release_body: ${{ steps.new_release.outputs.body }}
new_milestone_title: ${{ steps.new_milestone.outputs.title }}
new_milestone_description: ${{ steps.new_milestone.outputs.description }}
steps:
- name: Check pull request merged
id: merged_pr
Expand Down Expand Up @@ -79,50 +80,29 @@ jobs:
if: steps.merged_pr.outputs.status == 'yes'
id: new_milestone
run: |
echo "title=$(echo "${{ steps.new_release.outputs.name }}" | awk -F . '{print $1 "." $2+1 "." "0"}')" >> $GITHUB_OUTPUT
next_release_version=$(echo "${{ steps.new_release.outputs.name }}" | awk -F . '{print $1 "." $2+1 "." "0"}')
echo "title=${next_release_version}" >> $GITHUB_OUTPUT
lf='\n'
description="## $(echo ${next_release_version} | sed -E 's/^v(.+)/\1/g') (202X/XX/XX)${lf}"
description+="${lf}### New features${lf}"
description+="${lf}### Enhancements${lf}"
description+="${lf}### Bug fixes${lf}"
echo "description=${description}" >> $GITHUB_OUTPUT
- name: Notify preparation
run: |
echo "Pull request merged status: ${{ steps.merged_pr.outputs.status }}"
echo "New release name: ${{ steps.new_release.outputs.name }}"
echo "New release body: ${{ steps.new_release.outputs.body }}"
echo "New milestone title: ${{ steps.new_milestone.outputs.title }}"
code_coverage:
needs:
- preparation
if: needs.preparation.outputs.pull_request_merged_status == 'yes'
runs-on: ubuntu-latest
defaults:
run:
shell: bash
steps:
- name: Checkout branch
uses: actions/checkout@v4

- name: Set up node
uses: actions/setup-node@v4
with:
node-version: ${{ vars.DEFAULT_NODE_VERSION }}
cache: npm
cache-dependency-path: package-lock.json

- name: Set up npm
run: |
npm ci
- name: Publish code coverage
uses: paambaati/[email protected]
env:
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
with:
coverageCommand: npm run test
debug: true
echo "New milestone description: ${{ steps.new_milestone.outputs.description }}"
creation:
needs:
- preparation
- code_coverage
if: needs.preparation.outputs.pull_request_merged_status == 'yes'
runs-on: ubuntu-latest
defaults:
Expand Down Expand Up @@ -154,7 +134,8 @@ jobs:
const params = {
owner: context.repo.owner,
repo: context.repo.repo,
title: '${{ needs.preparation.outputs.new_milestone_title }}'
title: '${{ needs.preparation.outputs.new_milestone_title }}',
description: '${{ needs.preparation.outputs.new_milestone_description }}'
}
await github.rest.issues.createMilestone(params)
Expand Down
44 changes: 13 additions & 31 deletions .github/workflows/ready-for-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,14 @@ jobs:
outputs:
ready_for_release_labeled_status: ${{ steps.labeled_ready_for_release.outputs.status }}
steps:
- name: Filter labeled name
id: filter_labeled
run: |
if [[ "${{ github.event.label.name }}" =~ ^ready-for-release ]]; then
i=0
for matched in "${BASH_REMATCH[@]}"; do
if [ ${i} -eq 0 ]; then
echo "match=${matched}" >> $GITHUB_OUTPUT
else
echo "group${i}=${matched}" >> $GITHUB_OUTPUT
fi
i=$(expr ${i} + 1)
done
fi
- name: Check ready for release labeled
id: labeled_ready_for_release
run: |
if [ -n "${{ steps.filter_labeled.outputs.match }}" ] && [[ "${{ github.event.pull_request.head.ref }}" =~ ^release/.+ ]]; then
echo "status=yes" >> $GITHUB_OUTPUT
exit 0
if [[ "${{ github.event.label.name }}" =~ ^ready-for-release ]] && [ "${#BASH_REMATCH[@]}" -gt 0 ]; then
if [[ "${{ github.event.pull_request.head.ref }}" =~ ^release/.+ ]]; then
echo "status=yes" >> $GITHUB_OUTPUT
exit 0
fi
fi
echo "status=no" >> $GITHUB_OUTPUT
Expand All @@ -54,7 +41,7 @@ jobs:
shell: bash
outputs:
summarize_result: ${{ steps.summarize.outputs.result }}
list_up_outcomes: ${{ steps.list_up.outputs.outcomes }}
summarize_outcomes: ${{ steps.summarize.outputs.outcomes }}
steps:
- name: Checkout branch
uses: actions/checkout@v4
Expand Down Expand Up @@ -82,19 +69,14 @@ jobs:
run: |
npm run test
- name: List up outcomes
id: list_up
- name: Summarize result
id: summarize
run: |
outcomes="{}"
outcomes=$(echo "${outcomes}" | jq --argjson kv_pair "{\"lint\":\"${{ steps.lint.outcome }}\"}" '. + $kv_pair')
outcomes=$(echo "${outcomes}" | jq --argjson kv_pair "{\"test\":\"${{ steps.test.outcome }}\"}" '. + $kv_pair')
echo "outcomes=$(echo "${outcomes}" | jq -c)" >> $GITHUB_OUTPUT
- name: Summarize result
id: summarize
run: |
outcomes='${{ toJSON(fromJSON(steps.list_up.outputs.outcomes)) }}'
echo "outcomes=$(echo "${outcomes}" | jq -c)" >> $GITHUB_OUTPUT
if "$(echo "${outcomes}" | jq -r '[values[] == ("failure")] | any')"; then
echo "result=failure" >> $GITHUB_OUTPUT
Expand All @@ -103,13 +85,12 @@ jobs:
echo "result=skipped" >> $GITHUB_OUTPUT
exit 0
fi
echo "result=success" >> $GITHUB_OUTPUT
- name: Notify check application
run: |
echo "Summarize result: ${{ steps.summarize.outputs.result }}"
echo "List up outcomes: ${{ steps.list_up.outputs.outcomes }}"
echo "Summarize outcomes: ${{ steps.summarize.outputs.outcomes }}"
check_milestone:
needs:
Expand All @@ -124,8 +105,9 @@ jobs:
steps:
- name: Check milestone set
id: set_milestone
env:
milestone: ${{ toJSON(github.event.pull_request.milestone) }}
run: |
milestone=$(echo '${{ toJSON(github.event.pull_request.milestone) }}' | jq)
if [ "${milestone}" = "null" ]; then
echo "status=no" >> $GITHUB_OUTPUT
exit 0
Expand Down Expand Up @@ -185,7 +167,7 @@ jobs:
id: pr_review_comment
run: |
lf='\n'
outcomes='${{ toJSON(fromJSON(needs.check_application.outputs.list_up_outcomes)) }}'
outcomes='${{ toJSON(fromJSON(needs.check_application.outputs.summarize_outcomes)) }}'
message="## :information_source: リリース準備の結果通知"
message+="${lf}### :memo: 概要"
Expand Down
42 changes: 42 additions & 0 deletions .github/workflows/upload-coverage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Upload coverage

on:
push:
branches:
- main
- 'release/**'
tags:
- '!**'

jobs:
lint_and_test:
runs-on: ubuntu-latest
defaults:
run:
shell: bash
steps:
- name: Checkout branch
uses: actions/checkout@v4

- name: Set up node
uses: actions/setup-node@v4
with:
node-version: ${{ vars.DEFAULT_NODE_VERSION }}
cache: npm
cache-dependency-path: package-lock.json

- name: Set up npm
run: |
npm ci
- name: Exec lint command
run: |
npm run lint
- name: Publish code coverage
uses: paambaati/[email protected]
env:
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
with:
coverageCommand: npm run test
debug: true
47 changes: 33 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,29 @@ npm install hexo-tag-ogp-link-preview

Write like below to your hexo article markdown file:
```
{% link_preview url [target] [rel] %}
{% link_preview url [target] [rel] [loading] %}
[Content]
{% endlink_preview %}
```

or you are able to use "Named Parameter":

```
{% link_preview url [rel:{rel_value}] [target:{target_value}] [loading:{loading_value}] %}
[Content]
{% endlink_preview %}
```

### Tag arguments

| Name | Required? | Default | Description |
|----------|-----------|------------|--------------------------------------------------------------------------------------------------------------------|
| `url` | Yes | | This parameter is a target of the link preview. |
| `target` | No | `_blank` | Specify a `target` attribute of the anchor element. One of `_self`, `_blank`, `_parent`, or `_top`. |
| `rel` | No | `nofollow` | Specify a `rel` attribute of the anchor element. One of `external`, `nofollow`, `noopener`, `noreferrer`, `opener` |
Notice: All optionally parameters (except for the required `url` parameter) are able to use "Named Parameter".

| Name | Required? | Default | Description |
|-----------|-----------|------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `url` | Yes | | This parameter is a target of the link preview. |
| `target` | No | `_blank` | Specify a `target` attribute of the anchor element.<br>One of `_self`, `_blank`, `_parent`, or `_top`. |
| `rel` | No | `nofollow` | Specify a `rel` attribute of the anchor element.<br>One of `external`, `nofollow`, `noopener`, `noreferrer`, or `opener`. |
| `loading` | No | `lazy` | Specify a `loading` attribute of the image element.<br>One of `lazy`, `eager`, or `none`.<br>If specify a `none`, remove loading attribute from image element. |

### Tag content

Expand All @@ -51,21 +62,29 @@ link_preview:
Notice: All setting values are NOT required.
| Name | type | Default | Description |
|----------------------------|----------------------|----------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `class_name` | `string` or `object` | `link-preview` | If you are specified `string`, set a `class` attribute of the anchor element only. If you are specified `object`, set each a `class` attribute of the anchor element and the image element. |
| `class_name`.`anchor_link` | `string` | `link-preview` | Set a `class` attribute of the anchor element. |
| `class_name`.`image` | `string` | | Set a `class` attribute of the image element. If you are not specify (empty string, etc.), nothing to set. |
| `description_length` | `number` | `140` | It sliced to fit if a number of character of the `og:Description` exceeds the specified number value. |
| `disguise_crawler` | `boolean` | `true` | If scraper for OpenGraph want to disguise to crawler, set `true`. Otherwise, set to `false`. |
| Name | type | Default | Description |
|----------------------------|----------------------|----------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `class_name` | `string` or `object` | `link-preview` | If you are specified `string`, set a `class` attribute of the anchor element only.<br>If you are specified `object`, set each a `class` attribute of the anchor element and the image element. |
| `class_name`.`anchor_link` | `string` | `link-preview` | Set a `class` attribute of the anchor element. |
| `class_name`.`image` | `string` | | Set a `class` attribute of the image element.<br>If you are not specify (empty string, etc.), nothing to set. |
| `description_length` | `number` | `140` | It sliced to fit if a number of character of the `og:Description` exceeds the specified number value. |
| `disguise_crawler` | `boolean` | `true` | If scraper for OpenGraph want to disguise to crawler, set `true`.<br>Otherwise, set to `false`. |

## Example

Write a following to your hexo article markdown file:

```markdown
{% link_preview http://www.example.com/ loading:lazy %}
fallback Text
{% endlink_preview %}
```

When scraper get OpenGraph successfully, generated html like blow:
```html
<a href="http://www.example.com/" target="_blank" rel="nofollow" class="link-preview">
<div class="og-image">
<img src="https://www.example.com/image.png" class="not-gallery-item">
<img src="https://www.example.com/image.png" alt="example image" class="not-gallery-item" loading="lazy">
</div>
<div class="descriptions">
<div class="og-title">title text</div>
Expand Down
50 changes: 10 additions & 40 deletions lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,21 @@ function hasProperty(obj, key) {
return obj && Object.hasOwnProperty.call(obj, key);
}

function stringLength(text) {
if (typeof text !== 'string') {
throw new Error('text is not string type.');
}

const segmentation = new Intl.Segmenter('ja', { granularity: 'grapheme' });
return [...segmentation.segment(text)].length;
function hasTypeOfProperty(obj, key, type) {
return hasProperty(obj, key) && typeof obj[key] === type;
}

function stringSlice(text, start, end) {
if (typeof text !== 'string') {
throw new Error('text is not string type.');
}

const strLength = stringLength(text);

let startIndex = typeof start !== 'number' || isNaN(Number(start)) ? 0 : Number(start);
if (startIndex < 0) {
startIndex = Math.max(startIndex + strLength, 0);
}

let endIndex = typeof end !== 'number' || isNaN(Number(end)) ? strLength : Number(end);
if (endIndex >= strLength) {
endIndex = strLength;
} else if (endIndex < 0) {
endIndex = Math.max(endIndex + strLength, 0);
}

let strings = '';
function getObjectValueFrom(obj, key, type, defaultValue) {
return hasTypeOfProperty(obj, key, type) ? obj[key] || defaultValue : defaultValue;
}

if (startIndex >= strLength || endIndex <= startIndex) {
return strings;
}
const segmentation = new Intl.Segmenter('ja', { granularity: 'grapheme' });
[...segmentation.segment(text)]
.forEach((value, index) => {
if (startIndex <= index && index < endIndex) {
strings += value.segment;
}
});
return strings;
function getValidNumber(value, defaultValue) {
return typeof value !== 'number' || isNaN(Number(value)) ? defaultValue : Number(value);
}

module.exports = {
hasProperty,
stringLength,
stringSlice,
hasTypeOfProperty,
getObjectValueFrom,
getValidNumber,
};
Loading

0 comments on commit 8e955ce

Please sign in to comment.